DDC 0.0.0

a discrete domain computation library

chunk_common.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 <type_traits>
10#include <utility>
11
12#include <experimental/mdspan>
13
14#include <Kokkos_Core.hpp>
15
16#include "ddc/chunk_traits.hpp"
17#include "ddc/detail/macros.hpp"
18#include "ddc/discrete_domain.hpp"
19
20namespace ddc {
21
26template <class... QueryDDims, class ChunkType>
28{
29 static_assert(is_chunk_v<ChunkType>, "Not a chunk span type");
30 return chunk.template domain<QueryDDims...>();
31}
32
33template <class ElementType, class SupportType, class LayoutStridedPolicy>
35
36template <class ElementType, class... DDims, class LayoutStridedPolicy>
38{
39protected:
41 using internal_mdspan_type = std::experimental::mdspan<
43 std::experimental::dextents<std::size_t, sizeof...(DDims)>,
44 std::experimental::layout_stride>;
45
46public:
48
50 using allocation_mdspan_type = std::experimental::mdspan<
52 std::experimental::dextents<std::size_t, sizeof...(DDims)>,
54
55 using const_allocation_mdspan_type = std::experimental::mdspan<
56 const ElementType,
57 std::experimental::dextents<std::size_t, sizeof...(DDims)>,
59
61
62 using extents_type = typename allocation_mdspan_type::extents_type;
63
64 using layout_type = typename allocation_mdspan_type::layout_type;
65
66 using accessor_type = typename allocation_mdspan_type::accessor_type;
67
68 using mapping_type = typename allocation_mdspan_type::mapping_type;
69
70 using element_type = typename allocation_mdspan_type::element_type;
71
72 using value_type = typename allocation_mdspan_type::value_type;
73
74 using size_type = typename allocation_mdspan_type::size_type;
75
76 using data_handle_type = typename allocation_mdspan_type::data_handle_type;
77
78 using reference = typename allocation_mdspan_type::reference;
79
80 // ChunkCommon, ChunkSpan and Chunk need to access to m_internal_mdspan and m_domain of other template versions
81 template <class, class, class>
82 friend class ChunkCommon;
83
84 template <class, class, class, class>
85 friend class ChunkSpan;
86
87 template <class, class, class>
88 friend class Chunk;
89
90 static_assert(mapping_type::is_always_strided());
91
92protected:
95
98
99public:
100 static KOKKOS_FUNCTION constexpr int rank() noexcept
101 {
102 return extents_type::rank();
103 }
104
105 static KOKKOS_FUNCTION constexpr int rank_dynamic() noexcept
106 {
107 return extents_type::rank_dynamic();
108 }
109
110 static KOKKOS_FUNCTION constexpr size_type static_extent(std::size_t r) noexcept
111 {
112 return extents_type::static_extent(r);
113 }
114
116 {
117 return mapping_type::is_always_unique();
118 }
119
121 {
122 return mapping_type::is_always_exhaustive();
123 }
124
126 {
127 return mapping_type::is_always_strided();
128 }
129
130private:
131 template <class Mapping = mapping_type>
132 static KOKKOS_FUNCTION constexpr std::
133 enable_if_t<std::is_constructible_v<Mapping, extents_type>, internal_mdspan_type>
134 make_internal_mdspan(ElementType* ptr, mdomain_type const& domain)
135 {
136 if (domain.empty()) {
137 return internal_mdspan_type(
138 ptr,
139 std::experimental::layout_stride::mapping<extents_type>());
140 }
141 extents_type extents_r(::ddc::extents<DDims>(domain).value()...);
142 mapping_type mapping_r(extents_r);
143
144 extents_type extents_s((front<DDims>(domain) + ddc::extents<DDims>(domain)).uid()...);
145 std::array<std::size_t, sizeof...(DDims)> strides_s {
146 mapping_r.stride(type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>)...};
147 std::experimental::layout_stride::mapping<extents_type> mapping_s(extents_s, strides_s);
148 return internal_mdspan_type(ptr - mapping_s(front<DDims>(domain).uid()...), mapping_s);
149 }
150
151public:
153 {
154 return m_internal_mdspan.accessor();
155 }
156
158 {
159 return m_domain.extents();
160 }
161
162 template <class QueryDDim>
164 {
165 return m_domain.template extent<QueryDDim>();
166 }
167
169 {
170 return allocation_mdspan().size();
171 }
172
174 {
175 return allocation_mdspan().mapping();
176 }
177
179 {
180 return allocation_mdspan().is_unique();
181 }
182
184 {
185 return allocation_mdspan().is_exhaustive();
186 }
187
189 {
190 return allocation_mdspan().is_strided();
191 }
192
193 template <class QueryDDim>
195 {
196 return m_internal_mdspan.stride(type_seq_rank_v<QueryDDim, detail::TypeSeq<DDims...>>);
197 }
198
203 {
204 return m_domain;
205 }
206
210 template <class... QueryDDims>
212 {
213 return select<QueryDDims...>(domain());
214 }
215
216protected:
219
225 internal_mdspan_type internal_mdspan,
226 mdomain_type const& domain) noexcept
227 : m_internal_mdspan(std::move(internal_mdspan))
228 , m_domain(domain)
229 {
230 }
231
236 template <
237 class Mapping = mapping_type,
238 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
240 : m_internal_mdspan(make_internal_mdspan(ptr, domain))
241 , m_domain(domain)
242 {
243 // Handle the case where an allocation of size 0 returns a nullptr.
244 assert(domain.empty() || ((ptr != nullptr) && !domain.empty()));
245 }
246
250 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon const& other) = default;
251
256
258
263 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon& operator=(ChunkCommon const& other) = default;
264
270
275 {
276 ElementType* ptr = m_internal_mdspan.data_handle();
277 if (!m_domain.empty()) {
278 ptr += m_internal_mdspan.mapping()(front<DDims>(m_domain).uid()...);
279 }
280 return ptr;
281 }
282
287 {
288 return m_internal_mdspan;
289 }
290
295 {
297 extents_type extents_s(::ddc::extents<DDims>(m_domain).value()...);
298 if constexpr (std::is_same_v<LayoutStridedPolicy, std::experimental::layout_stride>) {
299 mapping_type map(extents_s, m_internal_mdspan.mapping().strides());
300 return allocation_mdspan_type(data_handle(), map);
301 } else {
303 return allocation_mdspan_type(data_handle(), map);
304 }
306 }
307};
308
309} // namespace ddc
static KOKKOS_FUNCTION constexpr int rank() noexcept
Definition chunk_common.hpp:100
std::experimental::mdspan< const ElementType, std::experimental::dextents< std::size_t, sizeof...(DDims)>, LayoutStridedPolicy > const_allocation_mdspan_type
Definition chunk_common.hpp:55
internal_mdspan_type m_internal_mdspan
The raw view of the data.
Definition chunk_common.hpp:94
mdomain_type m_domain
The mesh on which this chunk is defined.
Definition chunk_common.hpp:97
static KOKKOS_FUNCTION constexpr bool is_always_strided() noexcept
Definition chunk_common.hpp:125
KOKKOS_FUNCTION constexpr bool is_unique() const noexcept
Definition chunk_common.hpp:178
static KOKKOS_FUNCTION constexpr int rank_dynamic() noexcept
Definition chunk_common.hpp:105
KOKKOS_FUNCTION constexpr mdomain_type domain() const noexcept
Provide access to the domain on which this chunk is defined.
Definition chunk_common.hpp:202
std::experimental::mdspan< ElementType, std::experimental::dextents< std::size_t, sizeof...(DDims)>, std::experimental::layout_stride > internal_mdspan_type
the raw mdspan underlying this, with the same indexing (0 might no be dereferenceable)
Definition chunk_common.hpp:41
KOKKOS_FUNCTION constexpr bool is_exhaustive() const noexcept
Definition chunk_common.hpp:183
KOKKOS_FUNCTION constexpr size_type extent() const noexcept
Definition chunk_common.hpp:163
KOKKOS_FUNCTION constexpr mapping_type mapping() const noexcept
Definition chunk_common.hpp:173
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon & operator=(ChunkCommon const &other)=default
Copy-assigns a new value to this ChunkCommon, yields a new view to the same data.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon const &other)=default
Constructs a new ChunkCommon by copy, yields a new view to the same data.
KOKKOS_FUNCTION constexpr DiscreteVector< DDims... > extents() const noexcept
Definition chunk_common.hpp:157
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon()=default
Empty ChunkCommon.
typename allocation_mdspan_type::mapping_type mapping_type
Definition chunk_common.hpp:68
KOKKOS_FUNCTION constexpr ElementType * data_handle() const
Access to the underlying allocation pointer.
Definition chunk_common.hpp:274
KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
Provide a modifiable view of the data.
Definition chunk_common.hpp:294
std::experimental::mdspan< ElementType, std::experimental::dextents< std::size_t, sizeof...(DDims)>, LayoutStridedPolicy > allocation_mdspan_type
The dereferenceable part of the co-domain but with a different domain, starting at 0.
Definition chunk_common.hpp:50
KOKKOS_FUNCTION constexpr ChunkCommon(internal_mdspan_type internal_mdspan, mdomain_type const &domain) noexcept
Constructs a new ChunkCommon from scratch.
Definition chunk_common.hpp:224
KOKKOS_FUNCTION constexpr ChunkCommon(ElementType *ptr, mdomain_type const &domain)
Constructs a new ChunkCommon from scratch.
Definition chunk_common.hpp:239
typename allocation_mdspan_type::size_type size_type
Definition chunk_common.hpp:74
typename allocation_mdspan_type::data_handle_type data_handle_type
Definition chunk_common.hpp:76
typename allocation_mdspan_type::layout_type layout_type
Definition chunk_common.hpp:64
static KOKKOS_FUNCTION constexpr bool is_always_unique() noexcept
Definition chunk_common.hpp:115
typename allocation_mdspan_type::accessor_type accessor_type
Definition chunk_common.hpp:66
KOKKOS_FUNCTION constexpr internal_mdspan_type internal_mdspan() const
Provide a modifiable view of the data.
Definition chunk_common.hpp:286
KOKKOS_FUNCTION constexpr size_type stride() const
Definition chunk_common.hpp:194
static KOKKOS_FUNCTION constexpr size_type static_extent(std::size_t r) noexcept
Definition chunk_common.hpp:110
typename mdomain_type::discrete_element_type discrete_element_type
Definition chunk_common.hpp:60
typename allocation_mdspan_type::reference reference
Definition chunk_common.hpp:78
KOKKOS_FUNCTION constexpr accessor_type accessor() const
Definition chunk_common.hpp:152
typename allocation_mdspan_type::element_type element_type
Definition chunk_common.hpp:70
static KOKKOS_FUNCTION constexpr bool is_always_exhaustive() noexcept
Definition chunk_common.hpp:120
KOKKOS_FUNCTION constexpr size_type size() const noexcept
Definition chunk_common.hpp:168
typename allocation_mdspan_type::extents_type extents_type
Definition chunk_common.hpp:62
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon & operator=(ChunkCommon &&other)=default
Move-assigns a new value to this ChunkCommon.
KOKKOS_FUNCTION constexpr bool is_strided() const noexcept
Definition chunk_common.hpp:188
typename allocation_mdspan_type::value_type value_type
Definition chunk_common.hpp:72
KOKKOS_FUNCTION constexpr DiscreteDomain< QueryDDims... > domain() const noexcept
Provide access to the domain on which this chunk is defined.
Definition chunk_common.hpp:211
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon &&other)=default
Constructs a new ChunkCommon by move.
Definition discrete_domain.hpp:51
KOKKOS_FUNCTION constexpr bool empty() const noexcept
Definition discrete_domain.hpp:202
KOKKOS_FUNCTION constexpr mlength_type extents() const noexcept
Definition discrete_domain.hpp:142
A DiscreteVector is a vector in the discrete dimension.
Definition discrete_vector.hpp:254
The top-level namespace of DDC.
Definition aligned_allocator.hpp:11
KOKKOS_FUNCTION auto get_domain(ChunkType const &chunk) noexcept
Access the domain (or subdomain) of a view.
Definition chunk_common.hpp:27
constexpr bool enable_chunk
Definition chunk_traits.hpp:16
KOKKOS_FUNCTION constexpr DiscreteElementType const & uid(DiscreteElement< Tag > const &tuple) noexcept
Definition discrete_element.hpp:51
KOKKOS_FUNCTION constexpr DiscreteDomain< QueryDDims... > select(DiscreteDomain< DDims... > const &domain)
Definition discrete_domain.hpp:385
Definition chunk.hpp:21
Definition chunk_common.hpp:34
Definition chunk_span.hpp:30