DDC 0.8.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 "detail/kokkos.hpp"
16#include "detail/type_seq.hpp"
17#include "detail/type_traits.hpp"
18
19#include "chunk_common.hpp"
20#include "discrete_domain.hpp"
21#include "discrete_element.hpp"
22
23namespace ddc {
24
25template <class, class, class>
26class Chunk;
27
28template <
29 class ElementType,
30 class SupportType,
31 class LayoutStridedPolicy = Kokkos::layout_right,
32 class MemorySpace = Kokkos::DefaultHostExecutionSpace::memory_space>
33class ChunkSpan;
34
35template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
36inline constexpr bool
38 = true;
39
40template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
41inline constexpr bool
43 = true;
44
45template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
46class ChunkSpan : public ChunkCommon<ElementType, SupportType, LayoutStridedPolicy>
47{
48 static_assert(
49 std::is_same_v<LayoutStridedPolicy, Kokkos::layout_left>
50 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_right>
51 || std::is_same_v<LayoutStridedPolicy, Kokkos::layout_stride>,
52 "ChunkSpan only supports layout_left, layout_right or layout_stride");
53
54protected:
55 using base_type = ChunkCommon<ElementType, SupportType, LayoutStridedPolicy>;
56
57public:
58 /// type of a span of this full chunk
59 using span_type = ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace>;
60
61 /// type of a view of this full chunk
62 using view_type = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
63
64 using discrete_domain_type = typename base_type::discrete_domain_type;
65
66 using memory_space = MemorySpace;
67
68 /// The dereferenceable part of the co-domain but with a different domain, starting at 0
69 using allocation_mdspan_type = typename base_type::allocation_mdspan_type;
70
71 using const_allocation_mdspan_type = typename base_type::const_allocation_mdspan_type;
72
73 using discrete_element_type = typename discrete_domain_type::discrete_element_type;
74
75 using discrete_vector_type = typename discrete_domain_type::discrete_vector_type;
76
77 using extents_type = typename base_type::extents_type;
78
79 using layout_type = typename base_type::layout_type;
80
81 using accessor_type = typename base_type::accessor_type;
82
83 using mapping_type = typename base_type::mapping_type;
84
85 using element_type = typename base_type::element_type;
86
87 using value_type = typename base_type::value_type;
88
89 using size_type = typename base_type::size_type;
90
91 using data_handle_type = typename base_type::data_handle_type;
92
93 using reference = typename base_type::reference;
94
95 template <class, class, class, class>
96 friend class ChunkSpan;
97
98protected:
99 template <class QueryDDim, class... ODDims>
100 KOKKOS_FUNCTION static constexpr auto get_slicer_for(DiscreteVector<ODDims...> const& c)
101 {
102 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
103 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
104 return c.template get<QueryDDim>();
105 } else {
106 return Kokkos::full_extent;
107 }
108 DDC_IF_NVCC_THEN_POP
109 }
110
111 template <class QueryDDim, class... ODDims, class... OODDims>
112 KOKKOS_FUNCTION static constexpr auto get_slicer_for(
113 DiscreteDomain<ODDims...> const& c,
114 DiscreteDomain<OODDims...> const& origin)
115 {
116 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
117 if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
118 DiscreteDomain<QueryDDim> const c_slice(c);
119 DiscreteDomain<QueryDDim> const origin_slice(origin);
120 return std::pair<std::size_t, std::size_t>(
121 c_slice.front() - origin_slice.front(),
122 c_slice.back() + 1 - origin_slice.front());
123 } else {
124 return Kokkos::full_extent;
125 }
126 DDC_IF_NVCC_THEN_POP
127 }
128
129 template <class TypeSeq>
130 struct slicer;
131
132 template <class... DDims>
133 struct slicer<detail::TypeSeq<DDims...>>
134 {
135 template <class... ODDims>
136 KOKKOS_FUNCTION constexpr auto operator()(
137 allocation_mdspan_type const& span,
138 DiscreteVector<ODDims...> const& c) const
139 {
140 return Kokkos::submdspan(span, get_slicer_for<DDims>(c)...);
141 }
142
143 template <class... ODDims, class... OODDims>
144 KOKKOS_FUNCTION constexpr auto operator()(
145 allocation_mdspan_type const& span,
146 DiscreteDomain<ODDims...> const& c,
147 DiscreteDomain<OODDims...> const& origin) const
148 {
149 return Kokkos::submdspan(span, get_slicer_for<DDims>(c, origin)...);
150 }
151 };
152
153public:
154 /// Empty ChunkSpan
155 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan() = default;
156
157 /** Constructs a new ChunkSpan by copy, yields a new view to the same data
158 * @param other the ChunkSpan to copy
159 */
160 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan const& other) = default;
161
162 /** Constructs a new ChunkSpan by move
163 * @param other the ChunkSpan to move
164 */
165 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkSpan(ChunkSpan&& other) noexcept = default;
166
167 /** Forbids to construct a ChunkSpan from a rvalue of type Chunk.
168 */
169 template <
170 class OElementType,
171 class Allocator,
172 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
173 ChunkSpan(Chunk<OElementType, SupportType, Allocator>&& other) noexcept = delete;
174
175 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
176 * @param other the Chunk to view
177 */
178 template <
179 class OElementType,
180 class Allocator,
181 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
182 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
183 Chunk<OElementType, SupportType, Allocator>& other) noexcept
184 : base_type(other.m_allocation_mdspan, other.m_domain)
185 {
186 }
187
188 /** Constructs a new ChunkSpan from a Chunk, yields a new view to the same data
189 * @param other the Chunk to view
190 */
191 // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
192 template <
193 class OElementType,
194 class SFINAEElementType = ElementType,
195 class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
196 class Allocator,
197 class = std::enable_if_t<std::is_same_v<typename Allocator::memory_space, MemorySpace>>>
198 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
199 Chunk<OElementType, SupportType, Allocator> const& other) noexcept
200 : base_type(other.m_allocation_mdspan, other.m_domain)
201 {
202 }
203
204 /** Constructs a new ChunkSpan by copy of a chunk, yields a new view to the same data
205 * @param other the ChunkSpan to move
206 */
207 template <class OElementType>
208 KOKKOS_FUNCTION constexpr explicit ChunkSpan(
209 ChunkSpan<OElementType, SupportType, layout_type, MemorySpace> const& other) noexcept
210 : base_type(other.m_allocation_mdspan, other.m_domain)
211 {
212 }
213
214 /** Constructs a new ChunkSpan from scratch
215 * @param ptr the allocation pointer to the data
216 * @param domain the domain that sustains the view
217 */
218 template <
219 class Mapping = mapping_type,
220 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
221 KOKKOS_FUNCTION constexpr ChunkSpan(ElementType* const ptr, SupportType const& domain)
222 : base_type(ptr, domain)
223 {
224 }
225
226 /** Constructs a new ChunkSpan from scratch
227 * @param allocation_mdspan the allocation mdspan to the data
228 * @param domain the domain that sustains the view
229 */
230 KOKKOS_FUNCTION constexpr ChunkSpan(
231 allocation_mdspan_type allocation_mdspan,
232 SupportType const& domain)
233 : base_type(allocation_mdspan, domain)
234 {
235 for (std::size_t i = 0; i < SupportType::rank(); ++i) {
236 assert(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 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 assert(odomain.empty()
314 || (DiscreteDomain<QueryDDims...>(this->m_domain).contains(odomain.front())
315 && DiscreteDomain<QueryDDims...>(this->m_domain).contains(odomain.back())));
316 slicer<to_type_seq_t<SupportType>> const slicer;
317 auto subview = slicer(this->allocation_mdspan(), odomain, this->m_domain);
318 using layout_type = typename decltype(subview)::layout_type;
319 using extents_type = typename decltype(subview)::extents_type;
320 if constexpr (
321 std::is_same_v<layout_type, Kokkos::Experimental::layout_left_padded<>>
322 || std::is_same_v<layout_type, Kokkos::Experimental::layout_right_padded<>>) {
323 Kokkos::layout_stride::mapping<extents_type> const mapping_stride(subview.mapping());
324 Kokkos::mdspan<ElementType, extents_type, Kokkos::layout_stride> const
325 a(subview.data_handle(), mapping_stride);
326 return ChunkSpan<
327 ElementType,
328 decltype(this->m_domain.restrict_with(odomain)),
329 Kokkos::layout_stride,
330 memory_space>(a, this->m_domain.restrict_with(odomain));
331 } else {
332 return ChunkSpan<
333 ElementType,
334 decltype(this->m_domain.restrict_with(odomain)),
335 layout_type,
336 memory_space>(subview, this->m_domain.restrict_with(odomain));
337 }
338 }
339
340 /** Element access using a list of DiscreteElement
341 * @param delems discrete elements
342 * @return reference to this element
343 */
344 template <
345 class... DElems,
346 std::enable_if_t<detail::all_of_v<is_discrete_element_v<DElems>...>, int> = 0>
347 KOKKOS_FUNCTION constexpr reference operator()(DElems const&... delems) const noexcept
348 {
349 static_assert(
350 SupportType::rank() == (0 + ... + DElems::size()),
351 "Invalid number of dimensions");
352 assert(this->m_domain.contains(delems...));
353 return DDC_MDSPAN_ACCESS_OP(
354 this->m_allocation_mdspan,
355 detail::array(this->m_domain.distance_from_front(delems...)));
356 }
357
358 /** Element access using a list of DiscreteVector
359 * @param dvects discrete vectors
360 * @return reference to this element
361 */
362 template <
363 class... DVects,
364 std::enable_if_t<detail::all_of_v<is_discrete_vector_v<DVects>...>, int> = 0,
365 std::enable_if_t<sizeof...(DVects) != 0, int> = 0>
366 KOKKOS_FUNCTION constexpr reference operator()(DVects const&... dvects) const noexcept
367 {
368 static_assert(
369 SupportType::rank() == (0 + ... + DVects::size()),
370 "Invalid number of dimensions");
371 return DDC_MDSPAN_ACCESS_OP(
372 this->m_allocation_mdspan,
373 detail::array(discrete_vector_type(dvects...)));
374 }
375
376 /** Access to the underlying allocation pointer
377 * @return allocation pointer
378 */
379 KOKKOS_FUNCTION constexpr ElementType* data_handle() const
380 {
381 return base_type::data_handle();
382 }
383
384 /** Provide a mdspan on the memory allocation
385 * @return allocation mdspan
386 */
387 KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
388 {
389 return base_type::allocation_mdspan();
390 }
391
392 /** Provide a mdspan on the memory allocation
393 * @return allocation mdspan
394 */
395 KOKKOS_FUNCTION constexpr auto allocation_kokkos_view() const
396 {
397 auto s = this->allocation_mdspan();
398 auto kokkos_layout = detail::build_kokkos_layout(
399 s.extents(),
400 s.mapping(),
401 std::make_index_sequence<SupportType::rank()> {});
402 return Kokkos::View<
403 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
404 decltype(kokkos_layout),
405 MemorySpace>(s.data_handle(), kokkos_layout);
406 }
407
408 KOKKOS_FUNCTION constexpr view_type span_cview() const
409 {
410 return view_type(*this);
411 }
412
413 KOKKOS_FUNCTION constexpr span_type span_view() const
414 {
415 return *this;
416 }
417};
418
419template <class DataType, class... Properties, class SupportType>
421 Kokkos::View<DataType, Properties...> const& view,
422 SupportType domain)
423 -> ChunkSpan<
424 detail::kokkos_to_mdspan_element_t<
425 typename Kokkos::View<DataType, Properties...>::data_type>,
426 SupportType,
427 detail::kokkos_to_mdspan_layout_t<
428 typename Kokkos::View<DataType, Properties...>::array_layout>,
429 typename Kokkos::View<DataType, Properties...>::memory_space>;
430
431template <class ElementType, class SupportType, class Allocator>
432ChunkSpan(Chunk<ElementType, SupportType, Allocator>& other) -> ChunkSpan<
433 ElementType,
434 SupportType,
435 Kokkos::layout_right,
436 typename Allocator::memory_space>;
437
438template <class ElementType, class SupportType, class Allocator>
439ChunkSpan(Chunk<ElementType, SupportType, Allocator> const& other) -> ChunkSpan<
440 ElementType const,
441 SupportType,
442 Kokkos::layout_right,
443 typename Allocator::memory_space>;
444
445template <
446 class ElementType,
447 class SupportType,
448 class LayoutStridedPolicy = Kokkos::layout_right,
449 class MemorySpace = Kokkos::HostSpace>
450using ChunkView = ChunkSpan<ElementType const, SupportType, LayoutStridedPolicy, MemorySpace>;
451
452} // 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 >