13#include <Kokkos_Core.hpp>
15#include "ddc/chunk_common.hpp"
16#include "ddc/detail/kokkos.hpp"
17#include "ddc/discrete_domain.hpp"
18#include "ddc/discrete_element.hpp"
22template <
class,
class,
class>
28 class LayoutStridedPolicy = Kokkos::layout_right,
29 class MemorySpace = Kokkos::DefaultHostExecutionSpace::memory_space>
42template <
class ElementType,
class... DDims,
class LayoutStridedPolicy,
class MemorySpace>
47 std::is_same_v<LayoutStridedPolicy, Kokkos::layout_left>
48 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_right>
49 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_stride>,
50 "ChunkSpan only supports layout_left, layout_right or layout_stride");
56 using typename base_type::internal_mdspan_type;
70 using discrete_domain_type =
typename base_type::discrete_domain_type;
72 using mdomain_type [[deprecated(
"Use `discrete_domain_type` instead")]]
75 using memory_space = MemorySpace;
78 using allocation_mdspan_type =
typename base_type::allocation_mdspan_type;
80 using const_allocation_mdspan_type =
typename base_type::const_allocation_mdspan_type;
82 using discrete_element_type =
typename discrete_domain_type::discrete_element_type;
84 using extents_type =
typename base_type::extents_type;
86 using layout_type =
typename base_type::layout_type;
88 using accessor_type =
typename base_type::accessor_type;
90 using mapping_type =
typename base_type::mapping_type;
92 using element_type =
typename base_type::element_type;
94 using value_type =
typename base_type::value_type;
96 using size_type =
typename base_type::size_type;
98 using data_handle_type =
typename base_type::data_handle_type;
100 using reference =
typename base_type::reference;
102 template <
class,
class,
class,
class>
106 template <
class QueryDDim,
class... ODDims>
109 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
110 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
111 return (uid<QueryDDim>(c) - front<QueryDDim>(
this->m_domain).uid());
113 return Kokkos::full_extent;
118 template <
class QueryDDim,
class... ODDims>
121 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
122 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
123 return std::pair<std::size_t, std::size_t>(
124 front<QueryDDim>(c) - front<QueryDDim>(
this->m_domain),
125 back<QueryDDim>(c) + 1 - front<QueryDDim>(
this->m_domain));
127 return Kokkos::full_extent;
137
138
142
143
147
151 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
152 ChunkSpan(
Chunk<OElementType, discrete_domain_type, Allocator>&& other)
noexcept =
delete;
155
156
160 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
162 Chunk<OElementType, discrete_domain_type, Allocator>& other)
noexcept
163 : base_type(other.m_internal_mdspan, other.m_domain)
168
169
173 class SFINAEElementType = ElementType,
174 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
176 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
178 Chunk<OElementType, discrete_domain_type, Allocator>
const& other)
noexcept
179 : base_type(other.m_internal_mdspan, other.m_domain)
184
185
186 template <
class OElementType>
188 ChunkSpan<OElementType, discrete_domain_type, layout_type, MemorySpace>
const&
190 : base_type(other.m_internal_mdspan, other.m_domain)
195
196
197
199 class Mapping = mapping_type,
200 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>,
int> = 0>
202 : base_type(ptr, domain)
207
208
209
211 allocation_mdspan_type allocation_mdspan,
212 discrete_domain_type
const& domain)
214 assert(((allocation_mdspan.extent(type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)
215 ==
static_cast<std::size_t>(domain.
template extent<DDims>().value()))
217 if (!domain.empty()) {
218 extents_type
const extents_s((front<DDims>(domain) + extents<DDims>(domain)).uid()...);
219 std::array<std::size_t,
sizeof...(DDims)>
const strides_s {
220 allocation_mdspan.mapping().stride(
221 type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)...};
222 Kokkos::layout_stride::mapping<extents_type>
const mapping_s(extents_s, strides_s);
223 this->m_internal_mdspan = internal_mdspan_type(
224 allocation_mdspan.data_handle() - mapping_s(front<DDims>(domain).uid()...),
227 this->m_internal_mdspan = internal_mdspan_type(allocation_mdspan);
229 this->m_domain = domain;
233
234
235
236 template <
class KokkosView,
class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
238 KokkosView
const& view,
239 discrete_domain_type
const& domain)
noexcept
241 detail::build_mdspan(view, std::make_index_sequence<
sizeof...(DDims)> {}),
249
250
251
255
256
257
261
262 template <
class... QueryDDims>
264 DiscreteElement<QueryDDims...>
const& slice_spec)
const
266 auto subview = Kokkos::submdspan(
allocation_mdspan(), get_slicer_for<DDims>(slice_spec)...);
267 using layout_type =
typename decltype(subview)::layout_type;
268 using extents_type =
typename decltype(subview)::extents_type;
269 using detail::TypeSeq;
270 using OutTypeSeqDDims = type_seq_remove_t<TypeSeq<DDims...>, TypeSeq<QueryDDims...>>;
271 using OutDDom = detail::convert_type_seq_to_discrete_domain_t<OutTypeSeqDDims>;
273 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
274 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
275 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
276 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
277 a(subview.data_handle(), mapping_stride);
281 Kokkos::layout_stride,
282 memory_space>(a, OutDDom(
this->m_domain));
288 memory_space>(subview, OutDDom(
this->m_domain));
293
294 template <
class... QueryDDims>
298 using layout_type =
typename decltype(subview)::layout_type;
299 using extents_type =
typename decltype(subview)::extents_type;
301 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
302 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
303 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
304 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
305 a(subview.data_handle(), mapping_stride);
308 decltype(
this->m_domain.restrict(odomain)),
309 Kokkos::layout_stride,
310 memory_space>(a,
this->m_domain.restrict(odomain));
314 decltype(
this->m_domain.restrict(odomain)),
316 memory_space>(subview,
this->m_domain.restrict(odomain));
321
322
323
324 template <
class... DElems>
328 sizeof...(DDims) == (0 + ... + DElems::size()),
329 "Invalid number of dimensions");
330 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
331 assert(((select<DDims>(take<DDims>(delems...)) >= front<DDims>(
this->m_domain)) && ...));
332 assert(((select<DDims>(take<DDims>(delems...)) <= back<DDims>(
this->m_domain)) && ...));
333 return DDC_MDSPAN_ACCESS_OP(
this->m_internal_mdspan, uid<DDims>(take<DDims>(delems...))...);
337
338
341 return base_type::data_handle();
345
346
349 return base_type::allocation_mdspan();
353
354
357 auto s =
this->allocation_mdspan();
358 auto kokkos_layout = detail::build_kokkos_layout(
361 std::make_index_sequence<
sizeof...(DDims)> {});
363 detail::mdspan_to_kokkos_element_t<ElementType,
sizeof...(DDims)>,
364 decltype(kokkos_layout),
365 MemorySpace>(s.data_handle(), kokkos_layout);
370 return view_type(*
this);
379template <
class KokkosView,
class... DDims,
class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
382 detail::kokkos_to_mdspan_element_t<
typename KokkosView::
data_type>,
384 detail::kokkos_to_mdspan_layout_t<
typename KokkosView::
array_layout>,
390 class LayoutStridedPolicy = Kokkos::layout_right,
391 class MemorySpace = Kokkos::HostSpace>
392using ChunkView =
ChunkSpan<ElementType
const, SupportType, LayoutStridedPolicy, MemorySpace>;
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan & operator=(ChunkSpan const &other)=default
Copy-assigns a new value to this ChunkSpan, yields a new view to the same data.
KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
Provide a mdspan on the memory allocation.
KOKKOS_FUNCTION constexpr ElementType * data_handle() const
Access to the underlying allocation pointer.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan()=default
Empty ChunkSpan.
KOKKOS_FUNCTION constexpr reference operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
KOKKOS_FUNCTION constexpr ChunkSpan(Chunk< OElementType, discrete_domain_type, Allocator > &other) noexcept
Constructs a new ChunkSpan from a Chunk, yields a new view to the same data.
ChunkSpan(Chunk< OElementType, discrete_domain_type, Allocator > &&other) noexcept=delete
Forbids to construct a ChunkSpan from a rvalue of type Chunk.
KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteElement< ODDims... > const &c) const
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan &&other)=default
Constructs a new ChunkSpan by move.
KOKKOS_FUNCTION constexpr span_type span_view() const
KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
KOKKOS_FUNCTION constexpr auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Restrict to a subdomain.
KOKKOS_FUNCTION constexpr ChunkSpan(KokkosView const &view, discrete_domain_type const &domain) noexcept
Constructs a new ChunkSpan from scratch.
KOKKOS_DEFAULTED_FUNCTION ~ChunkSpan()=default
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan & operator=(ChunkSpan &&other)=default
Move-assigns a new value to this ChunkSpan.
KOKKOS_FUNCTION constexpr ChunkSpan(Chunk< OElementType, discrete_domain_type, Allocator > const &other) noexcept
Constructs a new ChunkSpan from a Chunk, yields a new view to the same data.
KOKKOS_FUNCTION constexpr auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
KOKKOS_FUNCTION constexpr ChunkSpan(ElementType *const ptr, discrete_domain_type const &domain)
Constructs a new ChunkSpan from scratch.
KOKKOS_FUNCTION constexpr ChunkSpan(ChunkSpan< OElementType, discrete_domain_type, layout_type, MemorySpace > const &other) noexcept
Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data.
KOKKOS_FUNCTION constexpr ChunkSpan(allocation_mdspan_type allocation_mdspan, discrete_domain_type const &domain)
Constructs a new ChunkSpan from scratch.
KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteDomain< ODDims... > const &c) const
KOKKOS_FUNCTION constexpr view_type span_cview() const
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const &other)=default
Constructs a new ChunkSpan by copy, yields a new view to the same data.
friend class DiscreteDomain
The top-level namespace of DDC.
constexpr bool enable_borrowed_chunk< ChunkSpan< ElementType, SupportType, LayoutStridedPolicy, MemorySpace > >
ChunkSpan(KokkosView const &view, DiscreteDomain< DDims... > domain) -> ChunkSpan< detail::kokkos_to_mdspan_element_t< typename KokkosView::data_type >, DiscreteDomain< DDims... >, detail::kokkos_to_mdspan_layout_t< typename KokkosView::array_layout >, typename KokkosView::memory_space >
constexpr bool enable_chunk< ChunkSpan< ElementType, SupportType, LayoutStridedPolicy, MemorySpace > >