12#include <Kokkos_Core.hpp>
14#include "detail/kokkos.hpp"
15#include "detail/type_seq.hpp"
16#include "detail/type_traits.hpp"
18#include "chunk_common.hpp"
19#include "discrete_domain.hpp"
20#include "discrete_element.hpp"
24template <
class,
class,
class>
30 class LayoutStridedPolicy = Kokkos::layout_right,
31 class MemorySpace = Kokkos::DefaultHostExecutionSpace::memory_space>
44template <
class ElementType,
class SupportType,
class LayoutStridedPolicy,
class MemorySpace>
48 std::is_same_v<LayoutStridedPolicy, Kokkos::layout_left>
49 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_right>
50 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_stride>,
51 "ChunkSpan only supports layout_left, layout_right or layout_stride");
54 using base_type =
ChunkCommon<ElementType, SupportType, LayoutStridedPolicy>;
58 using span_type =
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace>;
61 using view_type =
ChunkSpan<ElementType
const, SupportType, LayoutStridedPolicy, MemorySpace>;
63 using discrete_domain_type =
typename base_type::discrete_domain_type;
65 using memory_space = MemorySpace;
68 using allocation_mdspan_type =
typename base_type::allocation_mdspan_type;
70 using const_allocation_mdspan_type =
typename base_type::const_allocation_mdspan_type;
72 using discrete_element_type =
typename discrete_domain_type::discrete_element_type;
74 using discrete_vector_type =
typename discrete_domain_type::discrete_vector_type;
76 using extents_type =
typename base_type::extents_type;
78 using layout_type =
typename base_type::layout_type;
80 using accessor_type =
typename base_type::accessor_type;
82 using mapping_type =
typename base_type::mapping_type;
84 using element_type =
typename base_type::element_type;
86 using value_type =
typename base_type::value_type;
88 using size_type =
typename base_type::size_type;
90 using data_handle_type =
typename base_type::data_handle_type;
92 using reference =
typename base_type::reference;
94 template <
class,
class,
class,
class>
98 template <
class QueryDDim,
class... ODDims>
101 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
102 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
103 return c.
template get<QueryDDim>();
105 return Kokkos::full_extent;
110 template <
class QueryDDim,
class... ODDims,
class... OODDims>
115 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
116 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
119 return std::pair<std::size_t, std::size_t>(
120 c_slice.front() - origin_slice.front(),
121 c_slice.back() + 1 - origin_slice.front());
123 return Kokkos::full_extent;
128 template <
class TypeSeq>
131 template <
class... DDims>
132 struct slicer<detail::TypeSeq<DDims...>>
134 template <
class... ODDims>
135 KOKKOS_FUNCTION
constexpr auto operator()(
136 allocation_mdspan_type
const& span,
139 return Kokkos::submdspan(span, get_slicer_for<DDims>(c)...);
142 template <
class... ODDims,
class... OODDims>
143 KOKKOS_FUNCTION
constexpr auto operator()(
144 allocation_mdspan_type
const& span,
148 return Kokkos::submdspan(span, get_slicer_for<DDims>(c, origin)...);
157
158
162
163
167
171 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
172 ChunkSpan(
Chunk<OElementType, SupportType, Allocator>&& other)
noexcept =
delete;
175
176
180 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
182 Chunk<OElementType, SupportType, Allocator>& other)
noexcept
183 : base_type(other.m_allocation_mdspan, other.m_domain)
188
189
193 class SFINAEElementType = ElementType,
194 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
196 class = std::enable_if_t<std::is_same_v<
typename Allocator::memory_space, MemorySpace>>>
198 Chunk<OElementType, SupportType, Allocator>
const& other)
noexcept
199 : base_type(other.m_allocation_mdspan, other.m_domain)
204
205
206 template <
class OElementType>
208 ChunkSpan<OElementType, SupportType, layout_type, MemorySpace>
const& other)
noexcept
209 : base_type(other.m_allocation_mdspan, other.m_domain)
214
215
216
218 class Mapping = mapping_type,
219 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>,
int> = 0>
221 : base_type(ptr, domain)
226
227
228
230 allocation_mdspan_type allocation_mdspan,
231 SupportType
const& domain)
232 : base_type(allocation_mdspan, domain)
234 for (std::size_t i = 0; i < SupportType::rank(); ++i) {
236 allocation_mdspan.extent(i)
237 ==
static_cast<std::size_t>(detail::array(domain.extents())[i]))
242
243
244
245 template <
class KokkosView,
class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
248 detail::build_mdspan(view, std::make_index_sequence<SupportType::rank()> {}),
256
257
258
262
263
264
268
269 template <
class... QueryDDims>
271 DiscreteElement<QueryDDims...>
const& slice_spec)
const
273 using detail::TypeSeq;
274 using QueryDDom =
typename detail::RebindDomain<SupportType, TypeSeq<QueryDDims...>>::type;
275 KOKKOS_ASSERT((QueryDDom(
this->m_domain).contains(slice_spec)))
276 slicer<to_type_seq_t<SupportType>>
const slicer;
277 auto subview = slicer(
278 this->allocation_mdspan(),
279 QueryDDom(
this->m_domain).distance_from_front(slice_spec));
280 using layout_type =
typename decltype(subview)::layout_type;
281 using extents_type =
typename decltype(subview)::extents_type;
282 using OutTypeSeqDDims
283 = type_seq_remove_t<to_type_seq_t<SupportType>, TypeSeq<QueryDDims...>>;
284 using OutDDom =
typename detail::RebindDomain<SupportType, OutTypeSeqDDims>::type;
286 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
287 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
288 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
289 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
290 a(subview.data_handle(), mapping_stride);
294 Kokkos::layout_stride,
295 memory_space>(a, OutDDom(
this->m_domain));
301 memory_space>(subview, OutDDom(
this->m_domain));
306
309 class SFINAESupportType = SupportType,
310 std::enable_if_t<is_discrete_domain_v<SFINAESupportType>, std::nullptr_t> =
nullptr>
315 || (DiscreteDomain<QueryDDims...>(
this->m_domain).contains(odomain.front())
316 && DiscreteDomain<QueryDDims...>(
this->m_domain).contains(odomain.back())))
317 slicer<to_type_seq_t<SupportType>>
const slicer;
318 auto subview = slicer(
this->allocation_mdspan(), odomain,
this->m_domain);
319 using layout_type =
typename decltype(subview)::layout_type;
320 using extents_type =
typename decltype(subview)::extents_type;
322 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
323 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
324 Kokkos::layout_stride::mapping<extents_type>
const mapping_stride(subview.mapping());
325 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride>
const
326 a(subview.data_handle(), mapping_stride);
329 decltype(
this->m_domain.restrict_with(odomain)),
330 Kokkos::layout_stride,
331 memory_space>(a,
this->m_domain.restrict_with(odomain));
335 decltype(
this->m_domain.restrict_with(odomain)),
337 memory_space>(subview,
this->m_domain.restrict_with(odomain));
342
343
344
347 std::enable_if_t<detail::all_of_v<is_discrete_element_v<DElems>...>,
int> = 0>
351 SupportType::rank() == (0 + ... + DElems::size()),
352 "Invalid number of dimensions");
353 KOKKOS_ASSERT((
this->m_domain.contains(delems...)))
354 return DDC_MDSPAN_ACCESS_OP(
355 this->m_allocation_mdspan,
356 detail::array(
this->m_domain.distance_from_front(delems...)));
360
361
362
365 std::enable_if_t<detail::all_of_v<is_discrete_vector_v<DVects>...>,
int> = 0,
366 std::enable_if_t<
sizeof...(DVects) != 0,
int> = 0>
370 SupportType::rank() == (0 + ... + DVects::size()),
371 "Invalid number of dimensions");
372 return DDC_MDSPAN_ACCESS_OP(
373 this->m_allocation_mdspan,
374 detail::array(discrete_vector_type(dvects...)));
378
379
382 return base_type::data_handle();
386
387
390 return base_type::allocation_mdspan();
394
395
398 auto s =
this->allocation_mdspan();
399 auto kokkos_layout = detail::build_kokkos_layout(
402 std::make_index_sequence<SupportType::rank()> {});
404 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
405 decltype(kokkos_layout),
406 MemorySpace>(s.data_handle(), kokkos_layout);
411 return view_type(*
this);
420template <
class DataType,
class... Properties,
class SupportType>
422 Kokkos::View<DataType, Properties...>
const& view,
425 detail::kokkos_to_mdspan_element_t<
426 typename Kokkos::View<DataType, Properties...>::
data_type>,
428 detail::kokkos_to_mdspan_layout_t<
429 typename Kokkos::View<DataType, Properties...>::
array_layout>,
430 typename Kokkos::View<DataType, Properties...>::
memory_space>;
432template <
class ElementType,
class SupportType,
class Allocator>
436 Kokkos::layout_right,
439template <
class ElementType,
class SupportType,
class Allocator>
443 Kokkos::layout_right,
449 class LayoutStridedPolicy = Kokkos::layout_right,
450 class MemorySpace = Kokkos::HostSpace>
451using ChunkView =
ChunkSpan<ElementType
const, SupportType, LayoutStridedPolicy, MemorySpace>;
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan &&other) noexcept=default
Constructs a new ChunkSpan by move.
KOKKOS_FUNCTION constexpr view_type span_cview() const
KOKKOS_FUNCTION constexpr ChunkSpan(Chunk< OElementType, SupportType, Allocator > const &other) noexcept
Constructs a new ChunkSpan from a Chunk, yields a new view to the same data.
KOKKOS_FUNCTION constexpr ChunkSpan(allocation_mdspan_type allocation_mdspan, SupportType const &domain)
Constructs a new ChunkSpan from scratch.
KOKKOS_FUNCTION constexpr auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
KOKKOS_FUNCTION constexpr ChunkSpan(KokkosView const &view, SupportType const &domain) noexcept
Constructs a new ChunkSpan from scratch.
ChunkSpan(Chunk< OElementType, SupportType, Allocator > &&other) noexcept=delete
Forbids to construct a ChunkSpan from a rvalue of type Chunk.
KOKKOS_FUNCTION constexpr auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Restrict to a subdomain, only valid when SupportType is a DiscreteDomain.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const &other)=default
Constructs a new ChunkSpan by copy, yields a new view to the same data.
static KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteDomain< ODDims... > const &c, DiscreteDomain< OODDims... > const &origin)
KOKKOS_FUNCTION constexpr ChunkSpan(ElementType *const ptr, SupportType const &domain)
Constructs a new ChunkSpan from scratch.
static KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteVector< ODDims... > const &c)
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan()=default
Empty ChunkSpan.
KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
KOKKOS_FUNCTION constexpr ElementType * data_handle() const
Access to the underlying allocation pointer.
KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
Provide a mdspan on the memory allocation.
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 ChunkSpan(Chunk< OElementType, SupportType, Allocator > &other) noexcept
Constructs a new ChunkSpan from a Chunk, yields a new view to the same data.
KOKKOS_FUNCTION constexpr ChunkSpan(ChunkSpan< OElementType, SupportType, layout_type, MemorySpace > const &other) noexcept
Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data.
KOKKOS_DEFAULTED_FUNCTION ~ChunkSpan() noexcept=default
KOKKOS_FUNCTION constexpr span_type span_view() const
KOKKOS_FUNCTION constexpr reference operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan & operator=(ChunkSpan &&other) noexcept=default
Move-assigns a new value to this ChunkSpan.
KOKKOS_FUNCTION constexpr reference operator()(DVects const &... dvects) const noexcept
Element access using a list of DiscreteVector.
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
The top-level namespace of DDC.
constexpr bool enable_borrowed_chunk< ChunkSpan< ElementType, SupportType, LayoutStridedPolicy, MemorySpace > >
ChunkSpan(Chunk< ElementType, SupportType, Allocator > const &other) -> ChunkSpan< ElementType const, SupportType, Kokkos::layout_right, typename Allocator::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 > >
KOKKOS_DEDUCTION_GUIDE ChunkSpan(Kokkos::View< DataType, Properties... > const &view, SupportType domain) -> ChunkSpan< detail::kokkos_to_mdspan_element_t< typename Kokkos::View< DataType, Properties... >::data_type >, SupportType, detail::kokkos_to_mdspan_layout_t< typename Kokkos::View< DataType, Properties... >::array_layout >, typename Kokkos::View< DataType, Properties... >::memory_space >