14#include <Kokkos_Core.hpp>
16#include "detail/tagged_vector.hpp"
25template <
concepts::discrete_dimension... DDims>
33template <
class... Tags>
44concept sparse_discrete_domain = is_sparse_discrete_domain_v<T>;
50template <
class... Tags>
56template <ddc::concepts::discrete_dimension... DDims, ddc::concepts::discrete_dimension... ODDims>
62template <
class InputIt1,
class InputIt2>
63KOKKOS_FUNCTION
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
65 for (; first1 != last1; ++first1, ++first2) {
66 if (!(*first1 == *first2)) {
74template <
class ForwardIt,
class T = std::iterator_traits<ForwardIt>::value_type,
class Compare>
75KOKKOS_FUNCTION ForwardIt lower_bound(ForwardIt first, ForwardIt last, T
const& value, Compare comp)
78 typename std::iterator_traits<ForwardIt>::difference_type count = last - first;
81 typename std::iterator_traits<ForwardIt>::difference_type
const step = count / 2;
84 if (comp(*it, value)) {
95template <
class ForwardIt,
class T = std::iterator_traits<ForwardIt>::value_type,
class Compare>
96KOKKOS_FUNCTION
bool binary_search(ForwardIt first, ForwardIt last, T
const& value, Compare comp)
98 first = ::
ddc::detail::lower_bound(first, last, value, comp);
99 return (!(first == last) && !(comp(value, *first)));
102template <ddc::concepts::discrete_dimension DDim>
103Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> extract_uid(
104 Kokkos::DefaultExecutionSpace
const& exec_space,
105 Kokkos::View<DiscreteElement<DDim>*, Kokkos::SharedSpace>
const& input)
107 Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace> output(input.label(), input.size());
108 Kokkos::parallel_for(
109 "SparseDiscreteDomainCtor",
111 Kokkos::DefaultExecutionSpace,
112 Kokkos::IndexType<std::size_t>>(exec_space, 0, output.size()),
113 KOKKOS_LAMBDA(std::size_t
const i) { output(i) = input(i).uid(); });
119template <
concepts::discrete_dimension... DDims>
122 template <
concepts::discrete_dimension...>
126 type_seq_is_unique_v<
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;
138 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
140 return sizeof...(DDims);
146 template <
concepts::sparse_discrete_domain... DDoms>
148 : m_views(domains.m_views...)
153
154
156 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
158 Kokkos::DefaultExecutionSpace
const exec_space;
159 ((m_views[type_seq_rank_v<DDims,
TypeSeq<DDims...>>]
160 = detail::extract_uid(exec_space, views)),
162 exec_space.fence(
"SparseDiscreteDomainCtor");
176 template <
concepts::discrete_dimension... ODDims>
179 if constexpr ((std::is_same_v<DDims, ODDims> && ...)) {
180 if (
empty() && other.empty()) {
183 for (std::size_t i = 0; i < m_views.size(); ++i) {
184 if (m_views[i].size() != other.m_views[i].size()) {
188 equal(m_views[i].data(),
189 m_views[i].data() + m_views[i].size(),
190 other.m_views[i].data())) {
200#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
205 return !(*
this ==
other);
209 KOKKOS_FUNCTION
constexpr std::size_t
size()
const
211 return (1UL * ... * get<DDims>(m_views).size());
214 KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
const noexcept
216 return discrete_vector_type(get<DDims>(m_views).size()...);
224 template <
concepts::discrete_dimension QueryDDim>
230 KOKKOS_FUNCTION
constexpr discrete_element_type
front()
const noexcept
232 return discrete_element_type(get<DDims>(m_views)(0)...);
235 KOKKOS_FUNCTION
constexpr discrete_element_type
back()
const noexcept
237 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
261 discrete_vector_type n1,
262 discrete_vector_type n2)
const
267 KOKKOS_FUNCTION
constexpr DiscreteElement<DDims...>
operator()(
270 return DiscreteElement<DDims...>(get<DDims>(m_views)(get<DDims>(dvect))...);
273 template <
class... DElems>
274 KOKKOS_FUNCTION
bool contains(DElems
const&... delems)
const noexcept
277 sizeof...(DDims) == (0 + ... + DElems::size()),
278 "Invalid number of dimensions");
279 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
280 DiscreteElement<DDims...>
const delem(delems...);
281 for (std::size_t i = 0; i <
rank(); ++i) {
282 if (!detail::binary_search(
284 m_views[i].data() + m_views[i].size(),
285 detail::array(delem)[i],
293 template <
class... DElems>
295 DElems
const&... delems)
const noexcept
298 sizeof...(DDims) == (0 + ... + DElems::size()),
299 "Invalid number of dimensions");
300 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
301 KOKKOS_ASSERT(contains(delems...))
303 (detail::lower_bound(
304 get<DDims>(m_views).data(),
305 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
306 uid<DDims>(take<DDims>(delems...)),
308 - get<DDims>(m_views).data())...);
311 KOKKOS_FUNCTION
constexpr bool empty()
const noexcept
316 KOKKOS_FUNCTION
constexpr explicit operator bool()
321 KOKKOS_FUNCTION
auto begin()
const
322 requires(
sizeof...(DDims) == 1)
324 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
325 return get<DDim0>(m_views).data();
328 KOKKOS_FUNCTION
auto end()
const
329 requires(
sizeof...(DDims) == 1)
331 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
332 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
335 KOKKOS_FUNCTION
auto cbegin()
const
336 requires(
sizeof...(DDims) == 1)
338 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
339 return get<DDim0>(m_views).data();
342 KOKKOS_FUNCTION
auto cend()
const
343 requires(
sizeof...(DDims) == 1)
345 using DDim0 = std::tuple_element_t<0, std::tuple<DDims...>>;
346 return get<DDim0>(m_views).data() + get<DDim0>(m_views).size();
349 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
350 requires(
sizeof...(DDims) == 1)
355 KOKKOS_FUNCTION
constexpr decltype(
auto)
operator[](std::size_t n)
const
356 requires(
sizeof...(DDims) == 1)
365 template <
concepts::discrete_dimension...>
369 using discrete_element_type = DiscreteElement<>;
373 static KOKKOS_FUNCTION
constexpr std::size_t
rank()
381 template <
concepts::discrete_dimension... ODDims>
403#if !defined(__cpp_impl_three_way_comparison
) || __cpp_impl_three_way_comparison
< 201902L
407 return !(*
this ==
other);
411 static KOKKOS_FUNCTION
constexpr std::size_t
size()
416 static KOKKOS_FUNCTION
constexpr discrete_vector_type
extents()
noexcept
421 static KOKKOS_FUNCTION
constexpr discrete_element_type
front()
noexcept
426 static KOKKOS_FUNCTION
constexpr discrete_element_type
back()
noexcept
452 discrete_vector_type ,
453 discrete_vector_type )
const
458 KOKKOS_FUNCTION
constexpr DiscreteElement<>
operator()(
464 static KOKKOS_FUNCTION
bool contains()
noexcept
469 static KOKKOS_FUNCTION
bool contains(DiscreteElement<>)
noexcept
484 static KOKKOS_FUNCTION
constexpr bool empty()
noexcept
489 KOKKOS_FUNCTION
constexpr explicit operator bool()
495template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
504template <ddc::concepts::type_seq T>
505struct ConvertTypeSeqToSparseDiscreteDomain
509template <ddc::concepts::discrete_dimension... DDims>
510struct ConvertTypeSeqToSparseDiscreteDomain<
ddc::
TypeSeq<DDims...>>
515template <ddc::concepts::type_seq T>
516using convert_type_seq_to_sparse_discrete_domain_t = ConvertTypeSeqToSparseDiscreteDomain<T>::type;
521template <
concepts::discrete_dimension... DDimsA,
concepts::discrete_dimension... DDimsB>
526 using TagSeqA =
TypeSeq<DDimsA...>;
527 using TagSeqB =
TypeSeq<DDimsB...>;
529 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
530 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
536template <
concepts::discrete_dimension... DDimsB,
concepts::discrete_dimension... DDimsA>
540 using TagSeqA =
TypeSeq<DDimsA...>;
541 using TagSeqB =
TypeSeq<DDimsB...>;
543 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
544 return detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(DDom_a);
551 ddc::concepts::discrete_dimension DDim1,
552 ddc::concepts::discrete_dimension DDim2,
553 ddc::concepts::discrete_dimension DDimA,
554 ddc::concepts::discrete_dimension... DDimsB>
555KOKKOS_FUNCTION
constexpr std::conditional_t<
556 std::is_same_v<DDimA, DDim1>,
563 if constexpr (std::is_same_v<DDimA, DDim1>) {
576 concepts::discrete_dimension... DDimsA,
577 concepts::discrete_dimension... DDimsB>
583 using TagSeqA =
TypeSeq<DDimsA...>;
584 using TagSeqB =
TypeSeq<DDim1>;
585 using TagSeqC =
TypeSeq<DDim2>;
587 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
588 return ddc::detail::convert_type_seq_to_sparse_discrete_domain_t<type_seq_r>(
589 detail::replace_dim_of_1d<
596template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
603template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
604KOKKOS_FUNCTION
constexpr DiscreteElement<QueryDDims...>
front(
610template <
concepts::discrete_dimension... QueryDDims,
concepts::discrete_dimension... DDims>
611KOKKOS_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(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
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.