12#include <Kokkos_Core.hpp>
13#include <Kokkos_StdAlgorithms.hpp>
15#include "detail/kokkos.hpp"
16#include "detail/tagged_vector.hpp"
17#include "detail/type_seq.hpp"
19#include "discrete_element.hpp"
20#include "discrete_vector.hpp"
25struct SparseDiscreteDomainIterator;
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, T
const& 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, T
const& 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 type_seq_is_unique_v<detail::TypeSeq<DDims...>>,
127 "The dimensions of a SparseDiscreteDomain must be unique");
130 using discrete_element_type = DiscreteElement<DDims...>;
135 detail::TaggedVector<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>, DDims...> m_views;
140 return sizeof...(DDims);
146 template <
class... DDoms,
class = std::enable_if_t<(is_sparse_discrete_domain_v<DDoms> && ...)>>
148 : m_views(domains.m_views...)
153
154
156 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
158 ((m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>]
159 = Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>(views.label(), views.size())),
161 Kokkos::DefaultExecutionSpace
const execution_space;
162 ((Kokkos::Experimental::transform(
163 "SparseDiscreteDomainCtor",
166 m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>],
167 detail::GetUidFn<DDims>())),
169 execution_space.fence(
"SparseDiscreteDomainCtor");
183 template <
class... ODims>
186 if (
empty() && other.empty()) {
189 if (m_views.size() != other.m_views.size()) {
192 for (std::size_t i = 0; i < m_views.size(); ++i) {
193 if (m_views[i].size() != other.m_views[i].size()) {
197 equal(m_views[i].data(),
198 m_views[i].data() + m_views[i].size(),
199 other.m_views[i].data())) {
206#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
208 template <
class...
ODims>
211 return !(*
this ==
other);
217 return (1UL * ... * get<DDims>(m_views).size());
222 return discrete_vector_type(get<DDims>(m_views).size()...);
230 template <
class QueryDDim>
238 return discrete_element_type(get<DDims>(m_views)(0)...);
243 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
267 discrete_vector_type n1,
268 discrete_vector_type n2)
const
276 return m_views(get<DDims>(dvect)...);
279 template <
class... DElems>
283 sizeof...(DDims) == (0 + ... + DElems::size()),
284 "Invalid number of dimensions");
285 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
286 return (detail::binary_search(
287 get<DDims>(m_views).data(),
288 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
289 uid<DDims>(take<DDims>(delems...)),
294 template <
class... DElems>
296 DElems
const&... delems)
const noexcept
299 sizeof...(DDims) == (0 + ... + DElems::size()),
300 "Invalid number of dimensions");
301 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
302 KOKKOS_ASSERT((contains(delems...)))
304 (detail::lower_bound(
305 get<DDims>(m_views).data(),
306 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
307 uid<DDims>(take<DDims>(delems...)),
309 - get<DDims>(m_views).data())...);
323 std::size_t N =
sizeof...(DDims),
324 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
327 return Kokkos::Experimental::begin(get<DDim0>(m_views));
331 std::size_t N =
sizeof...(DDims),
332 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
335 return Kokkos::Experimental::end(get<DDim0>(m_views));
339 std::size_t N =
sizeof...(DDims),
340 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
343 return Kokkos::Experimental::cbegin(get<DDim0>(m_views));
347 std::size_t N =
sizeof...(DDims),
348 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
351 return Kokkos::Experimental::cend(get<DDim0>(m_views));
355 std::size_t N =
sizeof...(DDims),
356 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
363 std::size_t N =
sizeof...(DDims),
364 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
378 using discrete_element_type = DiscreteElement<>;
390 template <
class... ODDims>
413#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
417 return !(*
this ==
other);
509template <
class... QueryDDims,
class... DDims>
514 DiscreteElement<QueryDDims...>(domain.front()),
515 DiscreteElement<QueryDDims...>(domain.extents()));
521struct ConvertTypeSeqToSparseDiscreteDomain;
523template <
class... DDims>
524struct ConvertTypeSeqToSparseDiscreteDomain<detail::TypeSeq<DDims...>>
530using convert_type_seq_to_sparse_discrete_domain_t =
531 typename ConvertTypeSeqToSparseDiscreteDomain<T>::type;
536template <
class... DDimsA,
class... DDimsB>
541 using TagSeqA = detail::TypeSeq<DDimsA...>;
542 using TagSeqB = detail::TypeSeq<DDimsB...>;
544 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
545 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
551template <
class... DDimsB,
class... DDimsA>
555 using TagSeqA = detail::TypeSeq<DDimsA...>;
556 using TagSeqB = detail::TypeSeq<DDimsB...>;
558 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
559 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
565template <
typename DDim1,
typename DDim2,
typename DDimA,
typename... DDimsB>
566KOKKOS_FUNCTION
constexpr std::conditional_t<
567 std::is_same_v<DDimA, DDim1>,
574 if constexpr (std::is_same_v<DDimA, DDim1>) {
584template <
typename DDim1,
typename DDim2,
typename... DDimsA,
typename... DDimsB>
590 using TagSeqA = detail::TypeSeq<DDimsA...>;
591 using TagSeqB = detail::TypeSeq<DDim1>;
592 using TagSeqC = detail::TypeSeq<DDim2>;
594 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
595 return ddc::detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(
596 detail::replace_dim_of_1d<
603template <
class... QueryDDims,
class... DDims>
610template <
class... QueryDDims,
class... DDims>
617template <
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
KOKKOS_FUNCTION constexpr bool operator==(SparseDiscreteDomain const &other) const
static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_DEFAULTED_FUNCTION constexpr SparseDiscreteDomain()=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain(SparseDiscreteDomain< ODDims... > const &domain)
KOKKOS_FUNCTION constexpr DiscreteElement operator()(DiscreteVector<> const &) const noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_last(discrete_vector_type n) const
static KOKKOS_FUNCTION DiscreteVector distance_from_front() noexcept
static KOKKOS_FUNCTION constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr bool empty() noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove(discrete_vector_type n1, discrete_vector_type n2) const
static KOKKOS_FUNCTION DiscreteVector distance_from_front(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_first(discrete_vector_type n) const
static KOKKOS_FUNCTION constexpr discrete_element_type back() noexcept
static KOKKOS_FUNCTION bool contains() noexcept
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain const &x)=default
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain &&x)=default
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain const &x)=default
KOKKOS_DEFAULTED_FUNCTION ~SparseDiscreteDomain()=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_first(discrete_vector_type n) const
static KOKKOS_FUNCTION constexpr discrete_element_type front() noexcept
static KOKKOS_FUNCTION constexpr discrete_vector_type extents() noexcept
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr discrete_element_type back() const noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_first(discrete_vector_type n) const
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n) const
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain const &x)=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_last(discrete_vector_type n) const
KOKKOS_FUNCTION DiscreteVector< DDims... > distance_from_front(DElems const &... delems) const noexcept
KOKKOS_FUNCTION constexpr auto discrete_elements() const noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove(discrete_vector_type n1, discrete_vector_type n2) const
KOKKOS_FUNCTION constexpr SparseDiscreteDomain(DDoms const &... domains)
Construct a SparseDiscreteDomain by copies and merge of domains.
SparseDiscreteDomain(Kokkos::View< DiscreteElement< DDims > *, Kokkos::SharedSpace > const &... views)
Construct a SparseDiscreteDomain with Kokkos::View explicitly listing the discrete elements.
KOKKOS_FUNCTION auto cbegin() const
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n)
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr std::size_t size() const
KOKKOS_FUNCTION constexpr bool operator==(SparseDiscreteDomain< ODims... > const &other) const
KOKKOS_FUNCTION bool contains(DElems const &... delems) const noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_first(discrete_vector_type n) const
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain()=default
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr discrete_element_type front() const noexcept
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_FUNCTION constexpr bool empty() const noexcept
KOKKOS_FUNCTION constexpr DiscreteElement< DDims... > operator()(DiscreteVector< DDims... > const &dvect) const noexcept
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain const &x)=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_FUNCTION auto end() const
KOKKOS_FUNCTION auto cend() const
friend class SparseDiscreteDomain
KOKKOS_FUNCTION auto begin() const
KOKKOS_FUNCTION constexpr discrete_vector_type extents() const noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDim > extent() const noexcept
KOKKOS_DEFAULTED_FUNCTION ~SparseDiscreteDomain()=default
The top-level namespace of DDC.
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > back(SparseDiscreteDomain< DDims... > const &domain) noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain< QueryDDims... > select(SparseDiscreteDomain< DDims... > const &domain)
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDims... > extents(SparseDiscreteDomain< DDims... > const &domain) noexcept
KOKKOS_FUNCTION constexpr auto replace_dim_of(SparseDiscreteDomain< DDimsA... > const &DDom_a, SparseDiscreteDomain< DDimsB... > const &DDom_b) noexcept
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > front(SparseDiscreteDomain< DDims... > const &domain) noexcept
constexpr bool is_sparse_discrete_domain_v
KOKKOS_FUNCTION constexpr auto remove_dims_of(SparseDiscreteDomain< DDimsA... > const &DDom_a) noexcept
Remove the dimensions DDimsB from DDom_a.
KOKKOS_FUNCTION constexpr auto remove_dims_of(SparseDiscreteDomain< DDimsA... > const &DDom_a, SparseDiscreteDomain< DDimsB... > const &DDom_b) noexcept