DDC 0.9.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 <cstddef>
9#include <type_traits>
10#include <utility>
11
12#include <Kokkos_Core.hpp>
13
14#include "detail/kokkos.hpp"
15#include "detail/type_seq.hpp"
16#include "detail/type_traits.hpp"
17
18#include "chunk_common.hpp"
19#include "discrete_domain.hpp"
20#include "discrete_element.hpp"
21
22namespace ddc {
23
24template <class, class, class>
25class Chunk;
26
27template <
28 class ElementType,
29 class SupportType,
30 class LayoutStridedPolicy = Kokkos::layout_right,
31 class MemorySpace = Kokkos::DefaultHostExecutionSpace::memory_space>
32class ChunkSpan;
33
34template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
35inline constexpr bool
37 = true;
38
39template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
40inline constexpr bool
42 = true;
43
44template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
45class ChunkSpan : public ChunkCommon<ElementType, SupportType, LayoutStridedPolicy>
46{
47 static_assert(
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");
52
53protected:
54 using base_type = ChunkCommon<ElementType, SupportType, LayoutStridedPolicy>;
55
56public:
57 /// type of a span of this full chunk
58 using span_type = ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace>;
59
60 /// type of a view of this full chunk
61 using view_type = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
62
63 using discrete_domain_type = typename base_type::discrete_domain_type;
64
65 using memory_space = MemorySpace;
66
67 /// The dereferenceable part of the co-domain but with a different domain, starting at 0
68 using allocation_mdspan_type = typename base_type::allocation_mdspan_type;
69
70 using const_allocation_mdspan_type = typename base_type::const_allocation_mdspan_type;
71
72 using discrete_element_type = typename discrete_domain_type::discrete_element_type;
73
74 using discrete_vector_type = typename discrete_domain_type::discrete_vector_type;
75
76 using extents_type = typename base_type::extents_type;
77
78 using layout_type = typename base_type::layout_type;
79
80 using accessor_type = typename base_type::accessor_type;
81
82 using mapping_type = typename base_type::mapping_type;
83
84 using element_type = typename base_type::element_type;
85
86 using value_type = typename base_type::value_type;
87
88 using size_type = typename base_type::size_type;
89
90 using data_handle_type = typename base_type::data_handle_type;
91
92 using reference = typename base_type::reference;
93
94 template <class, class, class, class>
95 friend class ChunkSpan;
96
97protected:
98 template <class QueryDDim, class... ODDims>
99 KOKKOS_FUNCTION static constexpr auto get_slicer_for(DiscreteVector<ODDims...> const& c)
100 {
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>();
104 } else {
105 return Kokkos::full_extent;
106 }
107 DDC_IF_NVCC_THEN_POP
108 }
109
110 template <class QueryDDim, class... ODDims, class... OODDims>
111 KOKKOS_FUNCTION static constexpr auto get_slicer_for(
112 DiscreteDomain<ODDims...> const& c,
113 DiscreteDomain<OODDims...> const& origin)
114 {
115 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
116 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
117 DiscreteDomain<QueryDDim> const c_slice(c);
118 DiscreteDomain<QueryDDim> const origin_slice(origin);
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());
122 } else {
123 return Kokkos::full_extent;
124 }
125 DDC_IF_NVCC_THEN_POP
126 }
127
128 template <class TypeSeq>
129 struct slicer;
130
131 template <class... DDims>
132 struct slicer<detail::TypeSeq<DDims...>>
133 {
134 template <class... ODDims>
135 KOKKOS_FUNCTION constexpr auto operator()(
136 allocation_mdspan_type const& span,
137 DiscreteVector<ODDims...> const& c) const
138 {
139 return Kokkos::submdspan(span, get_slicer_for<DDims>(c)...);
140 }
141
142 template <class... ODDims, class... OODDims>
143 KOKKOS_FUNCTION constexpr auto operator()(
144 allocation_mdspan_type const& span,
145 DiscreteDomain<ODDims...> const& c,
146 DiscreteDomain<OODDims...> const& origin) const
147 {
148 return Kokkos::submdspan(span, get_slicer_for<DDims>(c, origin)...);
149 }
150 };
151
152public:
153 /// Empty ChunkSpan
154 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan() = default;
155
156 /** Constructs a new ChunkSpan by copy, yields a new view to the same data
157 * @param other the ChunkSpan to copy
158 */
159 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const& other) = default;
160
161 /** Constructs a new ChunkSpan by move
162 * @param other the ChunkSpan to move
163 */
164 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan&& other) noexcept = default;
165
166 /** Forbids to construct a ChunkSpan from a rvalue of type Chunk.
167 */
168 template <
169 class OElementType,
170 class Allocator,
171 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
172 ChunkSpan(Chunk<OElementType, SupportType, Allocator>&& other) noexcept = delete;
173
174 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
175 * @param other the Chunk to view
176 */
177 template <
178 class OElementType,
179 class Allocator,
180 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
181 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
182 Chunk<OElementType, SupportType, Allocator>& other) noexcept
183 : base_type(other.m_allocation_mdspan, other.m_domain)
184 {
185 }
186
187 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
188 * @param other the Chunk to view
189 */
190 // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
191 template <
192 class OElementType,
193 class SFINAEElementType = ElementType,
194 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
195 class Allocator,
196 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
197 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
198 Chunk<OElementType, SupportType, Allocator> const& other) noexcept
199 : base_type(other.m_allocation_mdspan, other.m_domain)
200 {
201 }
202
203 /** Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data
204 * @param other the ChunkSpan to move
205 */
206 template <class OElementType>
207 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
208 ChunkSpan<OElementType, SupportType, layout_type, MemorySpace> const& other) noexcept
209 : base_type(other.m_allocation_mdspan, other.m_domain)
210 {
211 }
212
213 /** Constructs a new ChunkSpan from scratch
214 * @param ptr the allocation pointer to the data
215 * @param domain the domain that sustains the view
216 */
217 template <
218 class Mapping = mapping_type,
219 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
220 KOKKOS_FUNCTION constexpr ChunkSpan(ElementType* const ptr, SupportType const& domain)
221 : base_type(ptr, domain)
222 {
223 }
224
225 /** Constructs a new ChunkSpan from scratch
226 * @param allocation_mdspan the allocation mdspan to the data
227 * @param domain the domain that sustains the view
228 */
229 KOKKOS_FUNCTION constexpr ChunkSpan(
230 allocation_mdspan_type allocation_mdspan,
231 SupportType const& domain)
232 : base_type(allocation_mdspan, domain)
233 {
234 for (std::size_t i = 0; i < SupportType::rank(); ++i) {
235 KOKKOS_ASSERT(
236 allocation_mdspan.extent(i)
237 == static_cast<std::size_t>(detail::array(domain.extents())[i]))
238 }
239 }
240
241 /** Constructs a new ChunkSpan from scratch
242 * @param view the Kokkos view
243 * @param domain the domain that sustains the view
244 */
245 template <class KokkosView, class = std::enable_if_t<Kokkos::is_view_v<KokkosView>>>
246 KOKKOS_FUNCTION constexpr ChunkSpan(KokkosView const& view, SupportType const& domain) noexcept
247 : ChunkSpan(
248 detail::build_mdspan(view, std::make_index_sequence<SupportType::rank()> {}),
249 domain)
250 {
251 }
252
253 KOKKOS_DEFAULTED_FUNCTION ~ChunkSpan() noexcept = default;
254
255 /** Copy-assigns a new value to this ChunkSpan, yields a new view to the same data
256 * @param other the ChunkSpan to copy
257 * @return *this
258 */
259 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan const& other) = default;
260
261 /** Move-assigns a new value to this ChunkSpan
262 * @param other the ChunkSpan to move
263 * @return *this
264 */
265 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan& operator=(ChunkSpan&& other) noexcept = default;
266
267 /** Slice out some dimensions
268 */
269 template <class... QueryDDims>
270 KOKKOS_FUNCTION constexpr auto operator[](
271 DiscreteElement<QueryDDims...> const& slice_spec) const
272 {
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;
285 if constexpr (
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);
291 return ChunkSpan<
292 ElementType,
293 OutDDom,
294 Kokkos::layout_stride,
295 memory_space>(a, OutDDom(this->m_domain));
296 } else {
297 return ChunkSpan<
298 ElementType,
299 OutDDom,
300 layout_type,
301 memory_space>(subview, OutDDom(this->m_domain));
302 }
303 }
304
305 /** Restrict to a subdomain, only valid when SupportType is a DiscreteDomain
306 */
307 template <
308 class... QueryDDims,
309 class SFINAESupportType = SupportType,
310 std::enable_if_t<is_discrete_domain_v<SFINAESupportType>, std::nullptr_t> = nullptr>
311 KOKKOS_FUNCTION constexpr auto operator[](DiscreteDomain<QueryDDims...> const& odomain) const
312 {
313 KOKKOS_ASSERT(
314 odomain.empty()
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;
321 if constexpr (
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);
327 return ChunkSpan<
328 ElementType,
329 decltype(this->m_domain.restrict_with(odomain)),
330 Kokkos::layout_stride,
331 memory_space>(a, this->m_domain.restrict_with(odomain));
332 } else {
333 return ChunkSpan<
334 ElementType,
335 decltype(this->m_domain.restrict_with(odomain)),
336 layout_type,
337 memory_space>(subview, this->m_domain.restrict_with(odomain));
338 }
339 }
340
341 /** Element access using a list of DiscreteElement
342 * @param delems discrete elements
343 * @return reference to this element
344 */
345 template <
346 class... DElems,
347 std::enable_if_t<detail::all_of_v<is_discrete_element_v<DElems>...>, int> = 0>
348 KOKKOS_FUNCTION constexpr reference operator()(DElems const&... delems) const noexcept
349 {
350 static_assert(
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...)));
357 }
358
359 /** Element access using a list of DiscreteVector
360 * @param dvects discrete vectors
361 * @return reference to this element
362 */
363 template <
364 class... DVects,
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>
367 KOKKOS_FUNCTION constexpr reference operator()(DVects const&... dvects) const noexcept
368 {
369 static_assert(
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...)));
375 }
376
377 /** Access to the underlying allocation pointer
378 * @return allocation pointer
379 */
380 KOKKOS_FUNCTION constexpr ElementType* data_handle() const
381 {
382 return base_type::data_handle();
383 }
384
385 /** Provide a mdspan on the memory allocation
386 * @return allocation mdspan
387 */
388 KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
389 {
390 return base_type::allocation_mdspan();
391 }
392
393 /** Provide a mdspan on the memory allocation
394 * @return allocation mdspan
395 */
396 KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
397 {
398 auto s = this->allocation_mdspan();
399 auto kokkos_layout = detail::build_kokkos_layout(
400 s.extents(),
401 s.mapping(),
402 std::make_index_sequence<SupportType::rank()> {});
403 return Kokkos::View<
404 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
405 decltype(kokkos_layout),
406 MemorySpace>(s.data_handle(), kokkos_layout);
407 }
408
409 KOKKOS_FUNCTION constexpr view_type span_cview() const
410 {
411 return view_type(*this);
412 }
413
414 KOKKOS_FUNCTION constexpr span_type span_view() const
415 {
416 return *this;
417 }
418};
419
420template <class DataType, class... Properties, class SupportType>
422 Kokkos::View<DataType, Properties...> const& view,
423 SupportType domain)
424 -> ChunkSpan<
425 detail::kokkos_to_mdspan_element_t<
426 typename Kokkos::View<DataType, Properties...>::data_type>,
427 SupportType,
428 detail::kokkos_to_mdspan_layout_t<
429 typename Kokkos::View<DataType, Properties...>::array_layout>,
430 typename Kokkos::View<DataType, Properties...>::memory_space>;
431
432template <class ElementType, class SupportType, class Allocator>
433ChunkSpan(Chunk<ElementType, SupportType, Allocator>& other) -> ChunkSpan<
434 ElementType,
435 SupportType,
436 Kokkos::layout_right,
437 typename Allocator::memory_space>;
438
439template <class ElementType, class SupportType, class Allocator>
440ChunkSpan(Chunk<ElementType, SupportType, Allocator> const& other) -> ChunkSpan<
441 ElementType const,
442 SupportType,
443 Kokkos::layout_right,
444 typename Allocator::memory_space>;
445
446template <
447 class ElementType,
448 class SupportType,
449 class LayoutStridedPolicy = Kokkos::layout_right,
450 class MemorySpace = Kokkos::HostSpace>
451using ChunkView = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
452
453} // namespace ddc
friend class ChunkSpan
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 Chunk
Definition chunk.hpp:81
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 >