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>
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)));
108Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> extract_uid(
109 Kokkos::DefaultExecutionSpace
const& exec_space,
110 Kokkos::View<DiscreteElement<DDim>*, Kokkos::SharedSpace>
const& input)
112 Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> output(input.label(), input.size());
113 Kokkos::parallel_for(
114 "SparseDiscreteDomainCtor",
116 Kokkos::DefaultExecutionSpace,
117 Kokkos::IndexType<std::size_t>>(exec_space, 0, output.size()),
118 KOKKOS_LAMBDA(std::size_t
const i) { output(i) = input(i).uid(); });
124template <
class... DDims>
131 type_seq_is_unique_v<detail::TypeSeq<DDims...>>,
132 "The dimensions of a SparseDiscreteDomain must be unique");
135 using discrete_element_type = DiscreteElement<DDims...>;
140 detail::TaggedVector<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>, DDims...> m_views;
143 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
145 return sizeof...(DDims);
151 template <
class... DDoms,
class = std::enable_if_t<(is_sparse_discrete_domain_v<DDoms> && ...)>>
153 : m_views(domains.m_views...)
158
159
161 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
163 Kokkos::DefaultExecutionSpace
const exec_space;
164 ((m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>]
165 = detail::extract_uid(exec_space, views)),
167 exec_space.fence(
"SparseDiscreteDomainCtor");
181 template <
class... ODims>
184 if constexpr ((std::is_same_v<DDims, ODims> && ...)) {
185 if (
empty() && other.empty()) {
188 for (std::size_t i = 0; i < m_views.size(); ++i) {
189 if (m_views[i].size() != other.m_views[i].size()) {
193 equal(m_views[i].data(),
194 m_views[i].data() + m_views[i].size(),
195 other.m_views[i].data())) {
205#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
207 template <
class...
ODims>
210 return !(*
this ==
other);
214 KOKKOS_FUNCTION
constexpr std::size_t
size()
const
216 return (1UL * ... * get<DDims>(m_views).size());
219 KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
const noexcept
221 return discrete_vector_type(get<DDims>(m_views).size()...);
229 template <
class QueryDDim>
235 KOKKOS_FUNCTION
constexpr discrete_element_type
front()
const noexcept
237 return discrete_element_type(get<DDims>(m_views)(0)...);
240 KOKKOS_FUNCTION
constexpr discrete_element_type
back()
const noexcept
242 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
266 discrete_vector_type n1,
267 discrete_vector_type n2)
const
272 KOKKOS_FUNCTION
constexpr DiscreteElement<DDims...>
operator()(
275 return DiscreteElement<DDims...>(get<DDims>(m_views)(get<DDims>(dvect))...);
278 template <
class... DElems>
279 KOKKOS_FUNCTION
bool contains(DElems
const&... delems)
const noexcept
282 sizeof...(DDims) == (0 + ... + DElems::size()),
283 "Invalid number of dimensions");
284 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
285 DiscreteElement<DDims...>
const delem(delems...);
286 for (std::size_t i = 0; i <
rank(); ++i) {
287 if (!detail::binary_search(
289 m_views[i].data() + m_views[i].size(),
290 detail::array(delem)[i],
298 template <
class... DElems>
300 DElems
const&... delems)
const noexcept
303 sizeof...(DDims) == (0 + ... + DElems::size()),
304 "Invalid number of dimensions");
305 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
306 KOKKOS_ASSERT(contains(delems...))
308 (detail::lower_bound(
309 get<DDims>(m_views).data(),
310 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
311 uid<DDims>(take<DDims>(delems...)),
313 - get<DDims>(m_views).data())...);
316 KOKKOS_FUNCTION
constexpr bool empty()
const noexcept
321 KOKKOS_FUNCTION
constexpr explicit operator bool()
327 std::size_t N =
sizeof...(DDims),
328 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
329 KOKKOS_FUNCTION
auto begin()
const
331 return get<DDim0>(m_views).data();
335 std::size_t N =
sizeof...(DDims),
336 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
337 KOKKOS_FUNCTION
auto end()
const
339 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
343 std::size_t N =
sizeof...(DDims),
344 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
345 KOKKOS_FUNCTION
auto cbegin()
const
347 return get<DDim0>(m_views).data();
351 std::size_t N =
sizeof...(DDims),
352 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
353 KOKKOS_FUNCTION
auto cend()
const
355 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
359 std::size_t N =
sizeof...(DDims),
360 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
361 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
367 std::size_t N =
sizeof...(DDims),
368 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
369 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
const
382 using discrete_element_type = DiscreteElement<>;
386 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
394 template <
class... ODDims>
416#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
420 return !(*
this ==
other);
424 static KOKKOS_FUNCTION
constexpr std::size_t
size()
429 static KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
noexcept
434 static KOKKOS_FUNCTION
constexpr discrete_element_type
front()
noexcept
439 static KOKKOS_FUNCTION
constexpr discrete_element_type
back()
noexcept
465 discrete_vector_type ,
466 discrete_vector_type )
const
471 KOKKOS_FUNCTION
constexpr DiscreteElement<>
operator()(
477 static KOKKOS_FUNCTION
bool contains()
noexcept
482 static KOKKOS_FUNCTION
bool contains(DiscreteElement<>)
noexcept
497 static KOKKOS_FUNCTION
constexpr bool empty()
noexcept
502 KOKKOS_FUNCTION
constexpr explicit operator bool()
508template <
class... QueryDDims,
class... DDims>
518struct ConvertTypeSeqToSparseDiscreteDomain;
520template <
class... DDims>
521struct ConvertTypeSeqToSparseDiscreteDomain<detail::TypeSeq<DDims...>>
527using convert_type_seq_to_sparse_discrete_domain_t =
528 typename ConvertTypeSeqToSparseDiscreteDomain<T>::type;
533template <
class... DDimsA,
class... DDimsB>
538 using TagSeqA = detail::TypeSeq<DDimsA...>;
539 using TagSeqB = detail::TypeSeq<DDimsB...>;
541 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
542 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
548template <
class... DDimsB,
class... DDimsA>
552 using TagSeqA = detail::TypeSeq<DDimsA...>;
553 using TagSeqB = detail::TypeSeq<DDimsB...>;
555 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
556 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
562template <
typename DDim1,
typename DDim2,
typename DDimA,
typename... DDimsB>
563KOKKOS_FUNCTION
constexpr std::conditional_t<
564 std::is_same_v<DDimA, DDim1>,
571 if constexpr (std::is_same_v<DDimA, DDim1>) {
581template <
typename DDim1,
typename DDim2,
typename... DDimsA,
typename... DDimsB>
587 using TagSeqA = detail::TypeSeq<DDimsA...>;
588 using TagSeqB = detail::TypeSeq<DDim1>;
589 using TagSeqC = detail::TypeSeq<DDim2>;
591 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
592 return ddc::detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(
593 detail::replace_dim_of_1d<
600template <
class... QueryDDims,
class... DDims>
607template <
class... QueryDDims,
class... DDims>
608KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
front(
614template <
class... QueryDDims,
class... DDims>
615KOKKOS_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_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 std::enable_if_t< is_periodic_sampling_v< DDim >, Real > step() noexcept
Spacing step of the mesh.
KOKKOS_FUNCTION std::enable_if_t< is_periodic_sampling_v< DDim >, Coordinate< typename DDim::continuous_dimension_type > > origin() noexcept
Lower bound index of the mesh.
constexpr bool is_non_uniform_point_sampling_v
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmax(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > back(SparseDiscreteDomain< DDims... > const &domain) noexcept
bool is_discrete_space_initialized() noexcept
KOKKOS_FUNCTION constexpr SparseDiscreteDomain< QueryDDims... > select(SparseDiscreteDomain< DDims... > const &domain)
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
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rlength(DiscreteDomain< DDim > const &d)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_left(DiscreteElement< DDim > i)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > rmin(DiscreteDomain< DDim > const &d)
constexpr bool is_periodic_sampling_v
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > coordinate(DiscreteElement< DDim > const &c)
KOKKOS_FUNCTION std::enable_if_t< is_periodic_sampling_v< DDim >, DiscreteElement< DDim > > front() noexcept
Lower bound index 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 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.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_right(DiscreteElement< DDim > i)
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.