DDC 0.1.0
Loading...
Searching...
No Matches
chunk_span.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <array>
8#include <cassert>
9#include <cstddef>
10#include <type_traits>
11#include <utility>
12
13#include <Kokkos_Core.hpp>
14
15#include "ddc/chunk_common.hpp"
16#include "ddc/detail/kokkos.hpp"
17#include "ddc/discrete_domain.hpp"
18#include "ddc/discrete_element.hpp"
19
20namespace ddc {
21
22template <class, class, class>
23class Chunk;
24
25template <
26 class ElementType,
27 class SupportType,
28 class LayoutStridedPolicy = Kokkos::layout_right,
29 class MemorySpace = Kokkos::DefaultHostExecutionSpace::memory_space>
30class ChunkSpan;
31
32template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
33inline constexpr bool
35 = true;
36
37template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
38inline constexpr bool
40 = true;
41
42template <class ElementType, class... DDims, class LayoutStridedPolicy, class MemorySpace>
43class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, MemorySpace>
44 : public ChunkCommon<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy>
45{
46 static_assert(
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");
51
52protected:
53 using base_type = ChunkCommon<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy>;
54
55 /// the raw mdspan underlying this, with the same indexing (0 might no be dereferenceable)
56 using typename base_type::internal_mdspan_type;
57
58public:
59 /// type of a span of this full chunk
60 using span_type
61 = ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, MemorySpace>;
62
63 /// type of a view of this full chunk
64 using view_type = ChunkSpan<
65 ElementType const,
66 DiscreteDomain<DDims...>,
67 LayoutStridedPolicy,
68 MemorySpace>;
69
70 using discrete_domain_type = typename base_type::discrete_domain_type;
71
72 using mdomain_type [[deprecated("Use `discrete_domain_type` instead")]]
73 = DiscreteDomain<DDims...>;
74
75 using memory_space = MemorySpace;
76
77 /// The dereferenceable part of the co-domain but with a different domain, starting at 0
78 using allocation_mdspan_type = typename base_type::allocation_mdspan_type;
79
80 using const_allocation_mdspan_type = typename base_type::const_allocation_mdspan_type;
81
82 using discrete_element_type = typename discrete_domain_type::discrete_element_type;
83
84 using extents_type = typename base_type::extents_type;
85
86 using layout_type = typename base_type::layout_type;
87
88 using accessor_type = typename base_type::accessor_type;
89
90 using mapping_type = typename base_type::mapping_type;
91
92 using element_type = typename base_type::element_type;
93
94 using value_type = typename base_type::value_type;
95
96 using size_type = typename base_type::size_type;
97
98 using data_handle_type = typename base_type::data_handle_type;
99
100 using reference = typename base_type::reference;
101
102 template <class, class, class, class>
103 friend class ChunkSpan;
104
105protected:
106 template <class QueryDDim, class... ODDims>
107 KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteElement<ODDims...> const& c) const
108 {
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());
112 } else {
113 return Kokkos::full_extent;
114 }
115 DDC_IF_NVCC_THEN_POP
116 }
117
118 template <class QueryDDim, class... ODDims>
119 KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteDomain<ODDims...> const& c) const
120 {
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));
126 } else {
127 return Kokkos::full_extent;
128 }
129 DDC_IF_NVCC_THEN_POP
130 }
131
132public:
133 /// Empty ChunkSpan
134 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan() = default;
135
136 /** Constructs a new ChunkSpan by copy, yields a new view to the same data
137 * @param other the ChunkSpan to copy
138 */
139 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const& other) = default;
140
141 /** Constructs a new ChunkSpan by move
142 * @param other the ChunkSpan to move
143 */
144 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan&& other) = default;
145
146 /** Forbids to construct a ChunkSpan from a rvalue of type Chunk.
147 */
148 template <
149 class OElementType,
150 class Allocator,
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;
153
154 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
155 * @param other the Chunk to view
156 */
157 template <
158 class OElementType,
159 class Allocator,
160 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
161 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
162 Chunk<OElementType, discrete_domain_type, Allocator>& other) noexcept
163 : base_type(other.m_internal_mdspan, other.m_domain)
164 {
165 }
166
167 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
168 * @param other the Chunk to view
169 */
170 // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
171 template <
172 class OElementType,
173 class SFINAEElementType = ElementType,
174 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
175 class Allocator,
176 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
177 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
178 Chunk<OElementType, discrete_domain_type, Allocator> const& other) noexcept
179 : base_type(other.m_internal_mdspan, other.m_domain)
180 {
181 }
182
183 /** Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data
184 * @param other the ChunkSpan to move
185 */
186 template <class OElementType>
187 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
188 ChunkSpan<OElementType, discrete_domain_type, layout_type, MemorySpace> const&
189 other) noexcept
190 : base_type(other.m_internal_mdspan, other.m_domain)
191 {
192 }
193
194 /** Constructs a new ChunkSpan from scratch
195 * @param ptr the allocation pointer to the data
196 * @param domain the domain that sustains the view
197 */
198 template <
199 class Mapping = mapping_type,
200 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
201 KOKKOS_FUNCTION constexpr ChunkSpan(ElementType* const ptr, discrete_domain_type const& domain)
202 : base_type(ptr, domain)
203 {
204 }
205
206 /** Constructs a new ChunkSpan from scratch
207 * @param allocation_mdspan the allocation mdspan to the data
208 * @param domain the domain that sustains the view
209 */
210 KOKKOS_FUNCTION constexpr ChunkSpan(
211 allocation_mdspan_type allocation_mdspan,
212 discrete_domain_type const& domain)
213 {
214 assert(((allocation_mdspan.extent(type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)
215 == static_cast<std::size_t>(domain.template extent<DDims>().value()))
216 && ...));
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()...),
225 mapping_s);
226 } else {
227 this->m_internal_mdspan = internal_mdspan_type(allocation_mdspan);
228 }
229 this->m_domain = domain;
230 }
231
232 /** Constructs a new ChunkSpan from scratch
233 * @param view the Kokkos view
234 * @param domain the domain that sustains the view
235 */
236 template <class KokkosView, class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
237 KOKKOS_FUNCTION constexpr ChunkSpan(
238 KokkosView const& view,
239 discrete_domain_type const& domain) noexcept
240 : ChunkSpan(
241 detail::build_mdspan(view, std::make_index_sequence<sizeof...(DDims)> {}),
242 domain)
243 {
244 }
245
247
248 /** Copy-assigns a new value to this ChunkSpan, yields a new view to the same data
249 * @param other the ChunkSpan to copy
250 * @return *this
251 */
252 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan const& other) = default;
253
254 /** Move-assigns a new value to this ChunkSpan
255 * @param other the ChunkSpan to move
256 * @return *this
257 */
258 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan&& other) = default;
259
260 /** Slice out some dimensions
261 */
262 template <class... QueryDDims>
263 KOKKOS_FUNCTION constexpr auto operator[](
264 DiscreteElement<QueryDDims...> const& slice_spec) const
265 {
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>;
272 if constexpr (
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);
278 return ChunkSpan<
279 ElementType,
280 OutDDom,
281 Kokkos::layout_stride,
282 memory_space>(a, OutDDom(this->m_domain));
283 } else {
284 return ChunkSpan<
285 ElementType,
286 OutDDom,
287 layout_type,
288 memory_space>(subview, OutDDom(this->m_domain));
289 }
290 }
291
292 /** Restrict to a subdomain
293 */
294 template <class... QueryDDims>
295 KOKKOS_FUNCTION constexpr auto operator[](DiscreteDomain<QueryDDims...> const& odomain) const
296 {
297 auto subview = Kokkos::submdspan(allocation_mdspan(), get_slicer_for<DDims>(odomain)...);
298 using layout_type = typename decltype(subview)::layout_type;
299 using extents_type = typename decltype(subview)::extents_type;
300 if constexpr (
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);
306 return ChunkSpan<
307 ElementType,
308 decltype(this->m_domain.restrict(odomain)),
309 Kokkos::layout_stride,
310 memory_space>(a, this->m_domain.restrict(odomain));
311 } else {
312 return ChunkSpan<
313 ElementType,
314 decltype(this->m_domain.restrict(odomain)),
315 layout_type,
316 memory_space>(subview, this->m_domain.restrict(odomain));
317 }
318 }
319
320 /** Element access using a list of DiscreteElement
321 * @param delems discrete elements
322 * @return reference to this element
323 */
324 template <class... DElems>
325 KOKKOS_FUNCTION constexpr reference operator()(DElems const&... delems) const noexcept
326 {
327 static_assert(
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...))...);
334 }
335
336 /** Access to the underlying allocation pointer
337 * @return allocation pointer
338 */
339 KOKKOS_FUNCTION constexpr ElementType* data_handle() const
340 {
341 return base_type::data_handle();
342 }
343
344 /** Provide a mdspan on the memory allocation
345 * @return allocation mdspan
346 */
347 KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
348 {
349 return base_type::allocation_mdspan();
350 }
351
352 /** Provide a mdspan on the memory allocation
353 * @return allocation mdspan
354 */
355 KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
356 {
357 auto s = this->allocation_mdspan();
358 auto kokkos_layout = detail::build_kokkos_layout(
359 s.extents(),
360 s.mapping(),
361 std::make_index_sequence<sizeof...(DDims)> {});
362 return Kokkos::View<
363 detail::mdspan_to_kokkos_element_t<ElementType, sizeof...(DDims)>,
364 decltype(kokkos_layout),
365 MemorySpace>(s.data_handle(), kokkos_layout);
366 }
367
368 KOKKOS_FUNCTION constexpr view_type span_cview() const
369 {
370 return view_type(*this);
371 }
372
373 KOKKOS_FUNCTION constexpr span_type span_view() const
374 {
375 return *this;
376 }
377};
378
379template <class KokkosView, class... DDims, class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
380ChunkSpan(KokkosView const& view, DiscreteDomain<DDims...> domain)
381 -> ChunkSpan<
382 detail::kokkos_to_mdspan_element_t<typename KokkosView::data_type>,
383 DiscreteDomain<DDims...>,
384 detail::kokkos_to_mdspan_layout_t<typename KokkosView::array_layout>,
385 typename KokkosView::memory_space>;
386
387template <
388 class ElementType,
389 class SupportType,
390 class LayoutStridedPolicy = Kokkos::layout_right,
391 class MemorySpace = Kokkos::HostSpace>
392using ChunkView = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
393
394} // namespace ddc
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 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 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_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 > >