16#include <Kokkos_Core.hpp>
18#include "detail/tagged_vector.hpp"
27template <
concepts::discrete_dimension... DDims>
35template <
class... Tags>
46concept sparse_discrete_domain = is_sparse_discrete_domain_v<T>;
52template <
class... Tags>
58template <ddc::concepts::discrete_dimension... DDims, ddc::concepts::discrete_dimension... ODDims>
64template <
class InputIt1,
class InputIt2>
65KOKKOS_FUNCTION
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
67 for (; first1 != last1; ++first1, ++first2) {
68 if (!(*first1 == *first2)) {
76template <
class ForwardIt,
class T = std::iterator_traits<ForwardIt>::value_type,
class Compare>
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)) {
97template <
class ForwardIt,
class T = std::iterator_traits<ForwardIt>::value_type,
class Compare>
98KOKKOS_FUNCTION
bool binary_search(ForwardIt first, ForwardIt last, T
const& value, Compare comp)
100 first = ::
ddc::detail::lower_bound(first, last, value, comp);
101 return (!(first == last) && !(comp(value, *first)));
104template <ddc::concepts::discrete_dimension DDim>
105Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> extract_uid(
106 Kokkos::DefaultExecutionSpace
const& exec_space,
107 Kokkos::View<DiscreteElement<DDim>*, Kokkos::SharedSpace>
const& input)
109 Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> output(input.label(), input.size());
110 Kokkos::parallel_for(
111 "SparseDiscreteDomainCtor",
113 Kokkos::DefaultExecutionSpace,
114 Kokkos::IndexType<std::size_t>>(exec_space, 0, output.size()),
115 KOKKOS_LAMBDA(std::size_t
const i) { output(i) = input(i).uid(); });
121template <
concepts::discrete_dimension... DDims>
124 template <
concepts::discrete_dimension...>
128 type_seq_is_unique_v<
TypeSeq<DDims...>>,
129 "The dimensions of a SparseDiscreteDomain must be unique");
132 using discrete_element_type = DiscreteElement<DDims...>;
137 detail::TaggedVector<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>, DDims...> m_views;
140 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
142 return sizeof...(DDims);
148 template <
concepts::sparse_discrete_domain... DDoms>
150 : m_views(domains.m_views...)
155
156
158 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
160 Kokkos::DefaultExecutionSpace
const exec_space;
161 ((m_views[type_seq_rank_v<DDims,
TypeSeq<DDims...>>]
162 = detail::extract_uid(exec_space, views)),
164 exec_space.fence(
"SparseDiscreteDomainCtor");
178 template <
concepts::discrete_dimension... ODDims>
181 if constexpr ((std::is_same_v<DDims, ODDims> && ...)) {
182 if (
empty() && other.empty()) {
185 for (std::size_t i = 0; i < m_views.size(); ++i) {
186 if (m_views[i].size() != other.m_views[i].size()) {
190 equal(m_views[i].data(),
191 m_views[i].data() + m_views[i].size(),
192 other.m_views[i].data())) {
202#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
207 return !(*
this ==
other);
211 KOKKOS_FUNCTION
constexpr std::size_t
size()
const
213 return (1UL * ... * get<DDims>(m_views).size());
216 KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
const noexcept
218 return discrete_vector_type(get<DDims>(m_views).size()...);
226 template <
concepts::discrete_dimension QueryDDim>
232 KOKKOS_FUNCTION
constexpr discrete_element_type
front()
const noexcept
234 return discrete_element_type(get<DDims>(m_views)(0)...);
237 KOKKOS_FUNCTION
constexpr discrete_element_type
back()
const noexcept
239 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
263 discrete_vector_type n1,
264 discrete_vector_type n2)
const
269 KOKKOS_FUNCTION
constexpr DiscreteElement<DDims...>
operator()(
272 return DiscreteElement<DDims...>(get<DDims>(m_views)(get<DDims>(dvect))...);
275 template <
class... DElems>
276 KOKKOS_FUNCTION
bool contains(DElems
const&... delems)
const noexcept
279 sizeof...(DDims) == (0 + ... + DElems::size()),
280 "Invalid number of dimensions");
281 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
282 DiscreteElement<DDims...>
const delem(delems...);
283 for (std::size_t i = 0; i <
rank(); ++i) {
284 if (!detail::binary_search(
286 m_views[i].data() + m_views[i].size(),
287 detail::array(delem)[i],
295 template <
class... DElems>
297 DElems
const&... delems)
const noexcept
300 sizeof...(DDims) == (0 + ... + DElems::size()),
301 "Invalid number of dimensions");
302 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
303 KOKKOS_ASSERT(contains(delems...))
305 (detail::lower_bound(
306 get<DDims>(m_views).data(),
307 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
308 uid<DDims>(take<DDims>(delems...)),
310 - get<DDims>(m_views).data())...);
313 KOKKOS_FUNCTION
constexpr bool empty()
const noexcept
318 KOKKOS_FUNCTION
constexpr explicit operator bool()
323 KOKKOS_FUNCTION
auto begin()
const
324 requires(
sizeof...(DDims) == 1)
326 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
327 return get<DDim0>(m_views).data();
330 KOKKOS_FUNCTION
auto end()
const
331 requires(
sizeof...(DDims) == 1)
333 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
334 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
337 KOKKOS_FUNCTION
auto cbegin()
const
338 requires(
sizeof...(DDims) == 1)
340 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
341 return get<DDim0>(m_views).data();
344 KOKKOS_FUNCTION
auto cend()
const
345 requires(
sizeof...(DDims) == 1)
347 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
348 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
351 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
352 requires(
sizeof...(DDims) == 1)
357 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
const
358 requires(
sizeof...(DDims) == 1)
367 template <
concepts::discrete_dimension...>
371 using discrete_element_type = DiscreteElement<>;
375 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
383 template <
concepts::discrete_dimension... ODDims>
405#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
409 return !(*
this ==
other);
413 static KOKKOS_FUNCTION
constexpr std::size_t
size()
418 static KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
noexcept
423 static KOKKOS_FUNCTION detail::TaggedVector<
424 Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>>
430 static KOKKOS_FUNCTION
constexpr discrete_element_type
front()
noexcept
435 static KOKKOS_FUNCTION
constexpr discrete_element_type
back()
noexcept
461 discrete_vector_type ,
462 discrete_vector_type )
const
467 KOKKOS_FUNCTION
constexpr DiscreteElement<>
operator()(
473 static KOKKOS_FUNCTION
bool contains()
noexcept
478 static KOKKOS_FUNCTION
bool contains(DiscreteElement<>)
noexcept
493 static KOKKOS_FUNCTION
constexpr bool empty()
noexcept
498 KOKKOS_FUNCTION
constexpr explicit operator bool()
504template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
513template <ddc::concepts::type_seq T>
514struct ConvertTypeSeqToSparseDiscreteDomain
518template <ddc::concepts::discrete_dimension... DDims>
519struct ConvertTypeSeqToSparseDiscreteDomain<
ddc::
TypeSeq<DDims...>>
524template <ddc::concepts::type_seq T>
525using convert_type_seq_to_sparse_discrete_domain_t = ConvertTypeSeqToSparseDiscreteDomain<T>::type;
530template <
concepts::discrete_dimension... DDimsA,
concepts::discrete_dimension... DDimsB>
535 using TagSeqA =
TypeSeq<DDimsA...>;
536 using TagSeqB =
TypeSeq<DDimsB...>;
538 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
539 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
545template <
concepts::discrete_dimension... DDimsB,
concepts::discrete_dimension... DDimsA>
549 using TagSeqA =
TypeSeq<DDimsA...>;
550 using TagSeqB =
TypeSeq<DDimsB...>;
552 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
553 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
560 ddc::concepts::discrete_dimension DDim1,
561 ddc::concepts::discrete_dimension DDim2,
562 ddc::concepts::discrete_dimension DDimA,
563 ddc::concepts::discrete_dimension... DDimsB>
564KOKKOS_FUNCTION
constexpr std::conditional_t<
565 std::is_same_v<DDimA, DDim1>,
572 if constexpr (std::is_same_v<DDimA, DDim1>) {
585 concepts::discrete_dimension... DDimsA,
586 concepts::discrete_dimension... DDimsB>
592 using TagSeqA =
TypeSeq<DDimsA...>;
593 using TagSeqB =
TypeSeq<DDim1>;
594 using TagSeqC =
TypeSeq<DDim2>;
596 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
597 return ddc::detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(
598 detail::replace_dim_of_1d<
605template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
612template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
613KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
front(
619template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
620KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
back(
628void print_sparse_discrete_domain(
631 std::span<DiscreteVectorElement
const> extents,
632 std::span<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>
const> elements);
637
638
639
640
641
642
643
644
645template <concepts::discrete_dimension... DDims>
648 detail::print_sparse_discrete_domain(
651 detail::array(domain.extents()),
652 domain.discrete_elements().array());
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(int argc, char **&argv)
ScopeGuard(ScopeGuard &&x) noexcept=delete
ScopeGuard & operator=(ScopeGuard &&x) noexcept=delete
ScopeGuard(ScopeGuard const &x)=delete
static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain remove(discrete_vector_type, discrete_vector_type) const
static KOKKOS_FUNCTION detail::TaggedVector< Kokkos::View< DiscreteElementType *, Kokkos::SharedSpace > > discrete_elements() noexcept
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 bool operator==(SparseDiscreteDomain< ODDims... > const &other) 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_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 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)
A compile-time sequence of types.