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 memory_space = MemorySpace;
75 using allocation_mdspan_type =
typename base_type::allocation_mdspan_type;
77 using const_allocation_mdspan_type =
typename base_type::const_allocation_mdspan_type;
79 using discrete_element_type =
typename discrete_domain_type::discrete_element_type;
81 using extents_type =
typename base_type::extents_type;
83 using layout_type =
typename base_type::layout_type;
85 using accessor_type =
typename base_type::accessor_type;
87 using mapping_type =
typename base_type::mapping_type;
89 using element_type =
typename base_type::element_type;
91 using value_type =
typename base_type::value_type;
93 using size_type =
typename base_type::size_type;
95 using data_handle_type =
typename base_type::data_handle_type;
97 using reference =
typename base_type::reference;
99 template <
class,
class,
class,
class>
104 allocation_mdspan_type
const& allocation_mdspan,
105 discrete_domain_type
const& domain)
107 if (!domain.empty()) {
108 extents_type
const extents_s((front<DDims>(domain) + extents<DDims>(domain)).uid()...);
109 std::array<std::size_t,
sizeof...(DDims)>
const strides_s {
110 allocation_mdspan.mapping().stride(
111 type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)...};
112 Kokkos::layout_stride::mapping<extents_type>
const mapping_s(extents_s, strides_s);
113 return internal_mdspan_type(
114 allocation_mdspan.data_handle() - mapping_s(front<DDims>(domain).uid()...),
118 return internal_mdspan_type(allocation_mdspan);
121 template <
class QueryDDim,
class... ODDims>
124 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
125 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
126 return (uid<QueryDDim>(c) - front<QueryDDim>(
this->m_domain).uid());
128 return Kokkos::full_extent;
133 template <
class QueryDDim,
class... ODDims>
136 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
137 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
138 return std::pair<std::size_t, std::size_t>(
139 front<QueryDDim>(c) - front<QueryDDim>(
this->m_domain),
140 back<QueryDDim>(c) + 1 - front<QueryDDim>(
this->m_domain));
142 return Kokkos::full_extent;
152
153
157
158
162
166 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
167 ChunkSpan(
Chunk<OElementType, discrete_domain_type, Allocator>&& other)
noexcept =
delete;
170
171
175 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
177 Chunk<OElementType, discrete_domain_type, Allocator>& other)
noexcept
178 : base_type(other.m_internal_mdspan, other.m_domain)
183
184
188 class SFINAEElementType = ElementType,
189 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
191 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
193 Chunk<OElementType, discrete_domain_type, Allocator>
const& other)
noexcept
194 : base_type(other.m_internal_mdspan, other.m_domain)
199
200
201 template <
class OElementType>
203 ChunkSpan<OElementType, discrete_domain_type, layout_type, MemorySpace>
const&
205 : base_type(other.m_internal_mdspan, other.m_domain)
210
211
212
214 class Mapping = mapping_type,
215 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>,
int> = 0>
217 : base_type(ptr, domain)
222
223
224
226 allocation_mdspan_type allocation_mdspan,
227 discrete_domain_type
const& domain)
230 assert(((allocation_mdspan.extent(type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)
231 ==
static_cast<std::size_t>(domain.
template extent<DDims>().value()))
236
237
238
239 template <
class KokkosView,
class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
241 KokkosView
const& view,
242 discrete_domain_type
const& domain)
noexcept
244 detail::build_mdspan(view, std::make_index_sequence<
sizeof...(DDims)> {}),
252
253
254
258
259
260
264
265 template <
class... QueryDDims>
267 DiscreteElement<QueryDDims...>
const& slice_spec)
const
269 auto subview = Kokkos::submdspan(
allocation_mdspan(), get_slicer_for<DDims>(slice_spec)...);
270 using layout_type =
typename decltype(subview)::layout_type;
271 using extents_type =
typename decltype(subview)::extents_type;
272 using detail::TypeSeq;
273 using OutTypeSeqDDims = type_seq_remove_t<TypeSeq<DDims...>, TypeSeq<QueryDDims...>>;
274 using OutDDom = detail::convert_type_seq_to_discrete_domain_t<OutTypeSeqDDims>;
276 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
277 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
278 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
279 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
280 a(subview.data_handle(), mapping_stride);
284 Kokkos::layout_stride,
285 memory_space>(a, OutDDom(
this->m_domain));
291 memory_space>(subview, OutDDom(
this->m_domain));
296
297 template <
class... QueryDDims>
301 using layout_type =
typename decltype(subview)::layout_type;
302 using extents_type =
typename decltype(subview)::extents_type;
304 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
305 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
306 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
307 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
308 a(subview.data_handle(), mapping_stride);
311 decltype(
this->m_domain.restrict_with(odomain)),
312 Kokkos::layout_stride,
313 memory_space>(a,
this->m_domain.restrict_with(odomain));
317 decltype(
this->m_domain.restrict_with(odomain)),
319 memory_space>(subview,
this->m_domain.restrict_with(odomain));
324
325
326
327 template <
class... DElems>
331 sizeof...(DDims) == (0 + ... + DElems::size()),
332 "Invalid number of dimensions");
333 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
334 assert(((DiscreteElement<DDims>(take<DDims>(delems...)) >= front<DDims>(
this->m_domain))
336 assert(((DiscreteElement<DDims>(take<DDims>(delems...)) <= back<DDims>(
this->m_domain))
338 return DDC_MDSPAN_ACCESS_OP(
this->m_internal_mdspan, uid<DDims>(take<DDims>(delems...))...);
342
343
346 return base_type::data_handle();
350
351
354 return base_type::allocation_mdspan();
358
359
362 auto s =
this->allocation_mdspan();
363 auto kokkos_layout = detail::build_kokkos_layout(
366 std::make_index_sequence<
sizeof...(DDims)> {});
368 detail::mdspan_to_kokkos_element_t<ElementType,
sizeof...(DDims)>,
369 decltype(kokkos_layout),
370 MemorySpace>(s.data_handle(), kokkos_layout);
375 return view_type(*
this);
384template <
class DataType,
class... Properties,
class... DDims>
386 Kokkos::View<DataType, Properties...>
const& view,
389 detail::kokkos_to_mdspan_element_t<
390 typename Kokkos::View<DataType, Properties...>::
data_type>,
392 detail::kokkos_to_mdspan_layout_t<
393 typename Kokkos::View<DataType, Properties...>::
array_layout>,
394 typename Kokkos::View<DataType, Properties...>::
memory_space>;
396template <
class ElementType,
class SupportType,
class Allocator>
401 Kokkos::layout_right,
404template <
class ElementType,
class SupportType,
class Allocator>
409 Kokkos::layout_right,
415 class LayoutStridedPolicy = Kokkos::layout_right,
416 class MemorySpace = Kokkos::HostSpace>
417using 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_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_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan &&other) noexcept=default
Constructs a new ChunkSpan by move.
KOKKOS_FUNCTION constexpr ChunkSpan(KokkosView const &view, discrete_domain_type const &domain) noexcept
Constructs a new ChunkSpan from scratch.
static KOKKOS_FUNCTION internal_mdspan_type build_internal_mdspan(allocation_mdspan_type const &allocation_mdspan, discrete_domain_type const &domain)
KOKKOS_DEFAULTED_FUNCTION ~ChunkSpan() noexcept=default
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_DEFAULTED_FUNCTION constexpr ChunkSpan & operator=(ChunkSpan &&other) noexcept=default
Move-assigns a new value to this ChunkSpan.
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 > >
KOKKOS_DEDUCTION_GUIDE ChunkSpan(Kokkos::View< DataType, Properties... > const &view, DiscreteDomain< DDims... > domain) -> ChunkSpan< detail::kokkos_to_mdspan_element_t< typename Kokkos::View< DataType, Properties... >::data_type >, DiscreteDomain< DDims... >, detail::kokkos_to_mdspan_layout_t< typename Kokkos::View< DataType, Properties... >::array_layout >, typename Kokkos::View< DataType, Properties... >::memory_space >
ChunkSpan(Chunk< ElementType, SupportType, Allocator > &other) -> ChunkSpan< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
constexpr bool enable_chunk< ChunkSpan< ElementType, SupportType, LayoutStridedPolicy, MemorySpace > >
ChunkSpan(Chunk< ElementType, SupportType, Allocator > const &other) -> ChunkSpan< const ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >