14#include <Kokkos_Core.hpp>
16#include "detail/tagged_vector.hpp"
17#include "detail/type_seq.hpp"
25struct SparseDiscreteDomainIterator;
27template <
class... DDims>
35template <
class... Tags>
46concept sparse_discrete_domain = is_sparse_discrete_domain_v<T>;
52template <
class... Tags>
55 using type = TypeSeq<Tags...>;
58template <
class T,
class U>
61template <
class... DDims,
class... ODDims>
67template <
class InputIt1,
class InputIt2>
68KOKKOS_FUNCTION
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
70 for (; first1 != last1; ++first1, ++first2) {
71 if (!(*first1 == *first2)) {
81 class T =
typename std::iterator_traits<ForwardIt>::value_type,
83KOKKOS_FUNCTION ForwardIt lower_bound(ForwardIt first, ForwardIt last, T
const& value, Compare comp)
86 typename std::iterator_traits<ForwardIt>::difference_type count = last - first;
89 typename std::iterator_traits<ForwardIt>::difference_type
const step = count / 2;
92 if (comp(*it, value)) {
105 class T =
typename std::iterator_traits<ForwardIt>::value_type,
107KOKKOS_FUNCTION
bool binary_search(ForwardIt first, ForwardIt last, T
const& value, Compare comp)
109 first = ::
ddc::detail::lower_bound(first, last, value, comp);
110 return (!(first == last) && !(comp(value, *first)));
114Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> extract_uid(
115 Kokkos::DefaultExecutionSpace
const& exec_space,
116 Kokkos::View<DiscreteElement<DDim>*, Kokkos::SharedSpace>
const& input)
118 Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> output(input.label(), input.size());
119 Kokkos::parallel_for(
120 "SparseDiscreteDomainCtor",
122 Kokkos::DefaultExecutionSpace,
123 Kokkos::IndexType<std::size_t>>(exec_space, 0, output.size()),
124 KOKKOS_LAMBDA(std::size_t
const i) { output(i) = input(i).uid(); });
130template <
class... DDims>
137 type_seq_is_unique_v<detail::TypeSeq<DDims...>>,
138 "The dimensions of a SparseDiscreteDomain must be unique");
141 using discrete_element_type = DiscreteElement<DDims...>;
146 detail::TaggedVector<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>, DDims...> m_views;
149 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
151 return sizeof...(DDims);
157 template <
concepts::sparse_discrete_domain... DDoms>
159 : m_views(domains.m_views...)
164
165
167 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
169 Kokkos::DefaultExecutionSpace
const exec_space;
170 ((m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>]
171 = detail::extract_uid(exec_space, views)),
173 exec_space.fence(
"SparseDiscreteDomainCtor");
187 template <
class... ODims>
190 if constexpr ((std::is_same_v<DDims, ODims> && ...)) {
191 if (
empty() && other.empty()) {
194 for (std::size_t i = 0; i < m_views.size(); ++i) {
195 if (m_views[i].size() != other.m_views[i].size()) {
199 equal(m_views[i].data(),
200 m_views[i].data() + m_views[i].size(),
201 other.m_views[i].data())) {
211#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
213 template <
class...
ODims>
216 return !(*
this ==
other);
220 KOKKOS_FUNCTION
constexpr std::size_t
size()
const
222 return (1UL * ... * get<DDims>(m_views).size());
225 KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
const noexcept
227 return discrete_vector_type(get<DDims>(m_views).size()...);
235 template <
class QueryDDim>
241 KOKKOS_FUNCTION
constexpr discrete_element_type
front()
const noexcept
243 return discrete_element_type(get<DDims>(m_views)(0)...);
246 KOKKOS_FUNCTION
constexpr discrete_element_type
back()
const noexcept
248 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
272 discrete_vector_type n1,
273 discrete_vector_type n2)
const
278 KOKKOS_FUNCTION
constexpr DiscreteElement<DDims...>
operator()(
281 return DiscreteElement<DDims...>(get<DDims>(m_views)(get<DDims>(dvect))...);
284 template <
class... DElems>
285 KOKKOS_FUNCTION
bool contains(DElems
const&... delems)
const noexcept
288 sizeof...(DDims) == (0 + ... + DElems::size()),
289 "Invalid number of dimensions");
290 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
291 DiscreteElement<DDims...>
const delem(delems...);
292 for (std::size_t i = 0; i <
rank(); ++i) {
293 if (!detail::binary_search(
295 m_views[i].data() + m_views[i].size(),
296 detail::array(delem)[i],
304 template <
class... DElems>
306 DElems
const&... delems)
const noexcept
309 sizeof...(DDims) == (0 + ... + DElems::size()),
310 "Invalid number of dimensions");
311 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
312 KOKKOS_ASSERT(contains(delems...))
314 (detail::lower_bound(
315 get<DDims>(m_views).data(),
316 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
317 uid<DDims>(take<DDims>(delems...)),
319 - get<DDims>(m_views).data())...);
322 KOKKOS_FUNCTION
constexpr bool empty()
const noexcept
327 KOKKOS_FUNCTION
constexpr explicit operator bool()
332 KOKKOS_FUNCTION
auto begin()
const
333 requires(
sizeof...(DDims) == 1)
335 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
336 return get<DDim0>(m_views).data();
339 KOKKOS_FUNCTION
auto end()
const
340 requires(
sizeof...(DDims) == 1)
342 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
343 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
346 KOKKOS_FUNCTION
auto cbegin()
const
347 requires(
sizeof...(DDims) == 1)
349 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
350 return get<DDim0>(m_views).data();
353 KOKKOS_FUNCTION
auto cend()
const
354 requires(
sizeof...(DDims) == 1)
356 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
357 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
360 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
361 requires(
sizeof...(DDims) == 1)
366 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
const
367 requires(
sizeof...(DDims) == 1)
380 using discrete_element_type = DiscreteElement<>;
384 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
392 template <
class... ODDims>
414#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
418 return !(*
this ==
other);
422 static KOKKOS_FUNCTION
constexpr std::size_t
size()
427 static KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
noexcept
432 static KOKKOS_FUNCTION
constexpr discrete_element_type
front()
noexcept
437 static KOKKOS_FUNCTION
constexpr discrete_element_type
back()
noexcept
463 discrete_vector_type ,
464 discrete_vector_type )
const
469 KOKKOS_FUNCTION
constexpr DiscreteElement<>
operator()(
475 static KOKKOS_FUNCTION
bool contains()
noexcept
480 static KOKKOS_FUNCTION
bool contains(DiscreteElement<>)
noexcept
495 static KOKKOS_FUNCTION
constexpr bool empty()
noexcept
500 KOKKOS_FUNCTION
constexpr explicit operator bool()
506template <
class... QueryDDims,
class... DDims>
516struct ConvertTypeSeqToSparseDiscreteDomain;
518template <
class... DDims>
519struct ConvertTypeSeqToSparseDiscreteDomain<detail::TypeSeq<DDims...>>
525using convert_type_seq_to_sparse_discrete_domain_t =
526 typename ConvertTypeSeqToSparseDiscreteDomain<T>::type;
531template <
class... DDimsA,
class... DDimsB>
536 using TagSeqA = detail::TypeSeq<DDimsA...>;
537 using TagSeqB = detail::TypeSeq<DDimsB...>;
539 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
540 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
546template <
class... DDimsB,
class... DDimsA>
550 using TagSeqA = detail::TypeSeq<DDimsA...>;
551 using TagSeqB = detail::TypeSeq<DDimsB...>;
553 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
554 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
560template <
typename DDim1,
typename DDim2,
typename DDimA,
typename... DDimsB>
561KOKKOS_FUNCTION
constexpr std::conditional_t<
562 std::is_same_v<DDimA, DDim1>,
569 if constexpr (std::is_same_v<DDimA, DDim1>) {
579template <
typename DDim1,
typename DDim2,
typename... DDimsA,
typename... DDimsB>
585 using TagSeqA = detail::TypeSeq<DDimsA...>;
586 using TagSeqB = detail::TypeSeq<DDim1>;
587 using TagSeqC = detail::TypeSeq<DDim2>;
589 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
590 return ddc::detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(
591 detail::replace_dim_of_1d<
598template <
class... QueryDDims,
class... DDims>
605template <
class... QueryDDims,
class... DDims>
606KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
front(
612template <
class... QueryDDims,
class... DDims>
613KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
back(
friend class DiscreteDomain
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector()=default
Impl & operator=(Impl &&x)=default
KOKKOS_FUNCTION Coordinate< CDim > origin() const noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION discrete_element_type front() const noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION Coordinate< CDim > coordinate(discrete_element_type const &icoord) const noexcept
Convert a mesh index into a position in CDim
Impl(Impl const &)=delete
KOKKOS_FUNCTION std::size_t n_period() const
Number of steps in a period.
Impl(Impl< DDim, OriginMemorySpace > const &impl)
Impl(Coordinate< CDim > origin, Real step, std::size_t n_period)
Construct a Impl from a point and a spacing step.
KOKKOS_FUNCTION Real step() const
Spacing step of the mesh.
Impl & operator=(Impl const &x)=delete
PeriodicSampling models a periodic discretization of the provided continuous dimension.
static std::tuple< typename DDim::template Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim > > init(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n, DiscreteVector< DDim > n_period)
Construct a Impl<Kokkos::HostSpace> and associated discrete_domain_type from a segment and a number ...
std::tuple< Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim > > init_ghosted(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n, DiscreteVector< DDim > n_period, DiscreteVector< DDim > n_ghosts_before, DiscreteVector< DDim > n_ghosts_after)
Construct a periodic DiscreteDomain from a segment and a number of points n.
std::tuple< Impl< DDim, Kokkos::HostSpace >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim >, DiscreteDomain< DDim > > init_ghosted(Coordinate< CDim > a, Coordinate< CDim > b, DiscreteVector< DDim > n, DiscreteVector< DDim > n_period, DiscreteVector< DDim > n_ghosts)
Construct a periodic DiscreteDomain from a segment and a number of points n.
ScopeGuard & operator=(ScopeGuard const &x)=delete
ScopeGuard(ScopeGuard &&x) noexcept=delete
ScopeGuard & operator=(ScopeGuard &&x) noexcept=delete
ScopeGuard(int, char **&)
ScopeGuard(ScopeGuard const &x)=delete
static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove(discrete_vector_type, discrete_vector_type) const
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_first(discrete_vector_type) const
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_DEFAULTED_FUNCTION constexpr SparseDiscreteDomain()=default
KOKKOS_FUNCTION constexpr DiscreteElement operator()(DiscreteVector<> const &) const noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_last(discrete_vector_type) const
static KOKKOS_FUNCTION DiscreteVector distance_from_front() noexcept
static KOKKOS_FUNCTION constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr bool empty() noexcept
static KOKKOS_FUNCTION DiscreteVector distance_from_front(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr bool operator==(SparseDiscreteDomain const &) const
static KOKKOS_FUNCTION constexpr discrete_element_type back() noexcept
static KOKKOS_FUNCTION bool contains() noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain take_last(discrete_vector_type) const
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain const &x)=default
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain & operator=(SparseDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove_first(discrete_vector_type) const
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain const &x)=default
KOKKOS_DEFAULTED_FUNCTION ~SparseDiscreteDomain()=default
static KOKKOS_FUNCTION constexpr discrete_element_type front() noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain(SparseDiscreteDomain< ODDims... > const &)
static KOKKOS_FUNCTION constexpr discrete_vector_type extents() noexcept
KOKKOS_DEFAULTED_FUNCTION SparseDiscreteDomain(SparseDiscreteDomain &&x)=default
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_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 decltype(auto) operator[](std::size_t n) const
SparseDiscreteDomain(Kokkos::View< DiscreteElement< DDims > *, Kokkos::SharedSpace > const &... views)
Construct a SparseDiscreteDomain with Kokkos::View explicitly listing the discrete elements.
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 decltype(auto) operator[](std::size_t n)
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 SparseDiscreteDomain(DDoms const &... domains)
Construct a SparseDiscreteDomain by copies and merge of domains.
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
friend class SparseDiscreteDomain
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.
constexpr bool is_non_uniform_point_sampling_v
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > back(SparseDiscreteDomain< DDims... > const &domain) noexcept
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rlength(DiscreteDomain< DDim > const &d)
bool is_discrete_space_initialized() noexcept
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > coordinate(DiscreteElement< DDim > const &c)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_left(DiscreteElement< DDim > i)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > origin() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION constexpr SparseDiscreteDomain< QueryDDims... > select(SparseDiscreteDomain< DDims... > const &domain)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmin(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_right(DiscreteElement< DDim > i)
void init_discrete_space(Args &&... args)
Initialize (emplace) a global singleton discrete space.
KOKKOS_FUNCTION constexpr auto remove_dims_of(SparseDiscreteDomain< DDimsA... > const &DDom_a, SparseDiscreteDomain< DDimsB... > const &) noexcept
detail::ddim_impl_t< DDim, Kokkos::HostSpace > const & host_discrete_space()
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_periodic_sampling_v
KOKKOS_FUNCTION Real step() noexcept
Spacing step of the mesh.
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 DiscreteElement< DDim > front() noexcept
Lower bound index of the mesh.
KOKKOS_FUNCTION detail::ddim_impl_t< DDim, MemorySpace > const & discrete_space()
Arg0 init_discrete_space(std::tuple< DDimImpl, Arg0 > &&a)
Move construct a global singleton discrete space and pass through the other argument.
std::tuple< Arg0, Arg1, Args... > init_discrete_space(std::tuple< DDimImpl, Arg0, Arg1, Args... > &&a)
Move construct a global singleton discrete space and pass through remaining arguments.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmax(DiscreteDomain< DDim > const &d)