DDC 0.4.1
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 memory_space = MemorySpace;
73
74 /// The dereferenceable part of the co-domain but with a different domain, starting at 0
75 using allocation_mdspan_type = typename base_type::allocation_mdspan_type;
76
77 using const_allocation_mdspan_type = typename base_type::const_allocation_mdspan_type;
78
79 using discrete_element_type = typename discrete_domain_type::discrete_element_type;
80
81 using extents_type = typename base_type::extents_type;
82
83 using layout_type = typename base_type::layout_type;
84
85 using accessor_type = typename base_type::accessor_type;
86
87 using mapping_type = typename base_type::mapping_type;
88
89 using element_type = typename base_type::element_type;
90
91 using value_type = typename base_type::value_type;
92
93 using size_type = typename base_type::size_type;
94
95 using data_handle_type = typename base_type::data_handle_type;
96
97 using reference = typename base_type::reference;
98
99 template <class, class, class, class>
100 friend class ChunkSpan;
101
102protected:
103 static KOKKOS_FUNCTION internal_mdspan_type build_internal_mdspan(
104 allocation_mdspan_type const& allocation_mdspan,
105 discrete_domain_type const& domain)
106 {
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()...),
115 mapping_s);
116 }
117
118 return internal_mdspan_type(allocation_mdspan);
119 }
120
121 template <class QueryDDim, class... ODDims>
122 KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteElement<ODDims...> const& c) const
123 {
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());
127 } else {
128 return Kokkos::full_extent;
129 }
130 DDC_IF_NVCC_THEN_POP
131 }
132
133 template <class QueryDDim, class... ODDims>
134 KOKKOS_FUNCTION constexpr auto get_slicer_for(DiscreteDomain<ODDims...> const& c) const
135 {
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));
141 } else {
142 return Kokkos::full_extent;
143 }
144 DDC_IF_NVCC_THEN_POP
145 }
146
147public:
148 /// Empty ChunkSpan
149 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan() = default;
150
151 /** Constructs a new ChunkSpan by copy, yields a new view to the same data
152 * @param other the ChunkSpan to copy
153 */
154 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const& other) = default;
155
156 /** Constructs a new ChunkSpan by move
157 * @param other the ChunkSpan to move
158 */
159 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan&& other) noexcept = default;
160
161 /** Forbids to construct a ChunkSpan from a rvalue of type Chunk.
162 */
163 template <
164 class OElementType,
165 class Allocator,
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;
168
169 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
170 * @param other the Chunk to view
171 */
172 template <
173 class OElementType,
174 class Allocator,
175 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
176 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
177 Chunk<OElementType, discrete_domain_type, Allocator>& other) noexcept
178 : base_type(other.m_internal_mdspan, other.m_domain)
179 {
180 }
181
182 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
183 * @param other the Chunk to view
184 */
185 // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
186 template <
187 class OElementType,
188 class SFINAEElementType = ElementType,
189 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
190 class Allocator,
191 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
192 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
193 Chunk<OElementType, discrete_domain_type, Allocator> const& other) noexcept
194 : base_type(other.m_internal_mdspan, other.m_domain)
195 {
196 }
197
198 /** Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data
199 * @param other the ChunkSpan to move
200 */
201 template <class OElementType>
202 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
203 ChunkSpan<OElementType, discrete_domain_type, layout_type, MemorySpace> const&
204 other) noexcept
205 : base_type(other.m_internal_mdspan, other.m_domain)
206 {
207 }
208
209 /** Constructs a new ChunkSpan from scratch
210 * @param ptr the allocation pointer to the data
211 * @param domain the domain that sustains the view
212 */
213 template <
214 class Mapping = mapping_type,
215 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
216 KOKKOS_FUNCTION constexpr ChunkSpan(ElementType* const ptr, discrete_domain_type const& domain)
217 : base_type(ptr, domain)
218 {
219 }
220
221 /** Constructs a new ChunkSpan from scratch
222 * @param allocation_mdspan the allocation mdspan to the data
223 * @param domain the domain that sustains the view
224 */
225 KOKKOS_FUNCTION constexpr ChunkSpan(
226 allocation_mdspan_type allocation_mdspan,
227 discrete_domain_type const& domain)
228 : base_type(build_internal_mdspan(allocation_mdspan, domain), domain)
229 {
230 assert(((allocation_mdspan.extent(type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)
231 == static_cast<std::size_t>(domain.template extent<DDims>().value()))
232 && ...));
233 }
234
235 /** Constructs a new ChunkSpan from scratch
236 * @param view the Kokkos view
237 * @param domain the domain that sustains the view
238 */
239 template <class KokkosView, class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
240 KOKKOS_FUNCTION constexpr ChunkSpan(
241 KokkosView const& view,
242 discrete_domain_type const& domain) noexcept
243 : ChunkSpan(
244 detail::build_mdspan(view, std::make_index_sequence<sizeof...(DDims)> {}),
245 domain)
246 {
247 }
248
249 KOKKOS_DEFAULTED_FUNCTION ~ChunkSpan() noexcept = default;
250
251 /** Copy-assigns a new value to this ChunkSpan, yields a new view to the same data
252 * @param other the ChunkSpan to copy
253 * @return *this
254 */
255 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan const& other) = default;
256
257 /** Move-assigns a new value to this ChunkSpan
258 * @param other the ChunkSpan to move
259 * @return *this
260 */
261 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan&& other) noexcept = default;
262
263 /** Slice out some dimensions
264 */
265 template <class... QueryDDims>
266 KOKKOS_FUNCTION constexpr auto operator[](
267 DiscreteElement<QueryDDims...> const& slice_spec) const
268 {
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>;
275 if constexpr (
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);
281 return ChunkSpan<
282 ElementType,
283 OutDDom,
284 Kokkos::layout_stride,
285 memory_space>(a, OutDDom(this->m_domain));
286 } else {
287 return ChunkSpan<
288 ElementType,
289 OutDDom,
290 layout_type,
291 memory_space>(subview, OutDDom(this->m_domain));
292 }
293 }
294
295 /** Restrict to a subdomain
296 */
297 template <class... QueryDDims>
298 KOKKOS_FUNCTION constexpr auto operator[](DiscreteDomain<QueryDDims...> const& odomain) const
299 {
300 auto subview = Kokkos::submdspan(allocation_mdspan(), get_slicer_for<DDims>(odomain)...);
301 using layout_type = typename decltype(subview)::layout_type;
302 using extents_type = typename decltype(subview)::extents_type;
303 if constexpr (
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);
309 return ChunkSpan<
310 ElementType,
311 decltype(this->m_domain.restrict_with(odomain)),
312 Kokkos::layout_stride,
313 memory_space>(a, this->m_domain.restrict_with(odomain));
314 } else {
315 return ChunkSpan<
316 ElementType,
317 decltype(this->m_domain.restrict_with(odomain)),
318 layout_type,
319 memory_space>(subview, this->m_domain.restrict_with(odomain));
320 }
321 }
322
323 /** Element access using a list of DiscreteElement
324 * @param delems discrete elements
325 * @return reference to this element
326 */
327 template <class... DElems>
328 KOKKOS_FUNCTION constexpr reference operator()(DElems const&... delems) const noexcept
329 {
330 static_assert(
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))
335 && ...));
336 assert(((DiscreteElement<DDims>(take<DDims>(delems...)) <= back<DDims>(this->m_domain))
337 && ...));
338 return DDC_MDSPAN_ACCESS_OP(this->m_internal_mdspan, uid<DDims>(take<DDims>(delems...))...);
339 }
340
341 /** Access to the underlying allocation pointer
342 * @return allocation pointer
343 */
344 KOKKOS_FUNCTION constexpr ElementType* data_handle() const
345 {
346 return base_type::data_handle();
347 }
348
349 /** Provide a mdspan on the memory allocation
350 * @return allocation mdspan
351 */
352 KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
353 {
354 return base_type::allocation_mdspan();
355 }
356
357 /** Provide a mdspan on the memory allocation
358 * @return allocation mdspan
359 */
360 KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
361 {
362 auto s = this->allocation_mdspan();
363 auto kokkos_layout = detail::build_kokkos_layout(
364 s.extents(),
365 s.mapping(),
366 std::make_index_sequence<sizeof...(DDims)> {});
367 return Kokkos::View<
368 detail::mdspan_to_kokkos_element_t<ElementType, sizeof...(DDims)>,
369 decltype(kokkos_layout),
370 MemorySpace>(s.data_handle(), kokkos_layout);
371 }
372
373 KOKKOS_FUNCTION constexpr view_type span_cview() const
374 {
375 return view_type(*this);
376 }
377
378 KOKKOS_FUNCTION constexpr span_type span_view() const
379 {
380 return *this;
381 }
382};
383
384template <class DataType, class... Properties, class... DDims>
386 Kokkos::View<DataType, Properties...> const& view,
387 DiscreteDomain<DDims...> domain)
388 -> ChunkSpan<
389 detail::kokkos_to_mdspan_element_t<
390 typename Kokkos::View<DataType, Properties...>::data_type>,
391 DiscreteDomain<DDims...>,
392 detail::kokkos_to_mdspan_layout_t<
393 typename Kokkos::View<DataType, Properties...>::array_layout>,
394 typename Kokkos::View<DataType, Properties...>::memory_space>;
395
396template <class ElementType, class SupportType, class Allocator>
397ChunkSpan(Chunk<ElementType, SupportType, Allocator>& other)
398 -> ChunkSpan<
399 ElementType,
400 SupportType,
401 Kokkos::layout_right,
402 typename Allocator::memory_space>;
403
404template <class ElementType, class SupportType, class Allocator>
405ChunkSpan(Chunk<ElementType, SupportType, Allocator> const& other)
406 -> ChunkSpan<
407 const ElementType,
408 SupportType,
409 Kokkos::layout_right,
410 typename Allocator::memory_space>;
411
412template <
413 class ElementType,
414 class SupportType,
415 class LayoutStridedPolicy = Kokkos::layout_right,
416 class MemorySpace = Kokkos::HostSpace>
417using ChunkView = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
418
419} // 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_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_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_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 >