DDC 0.7.0
Loading...
Searching...
No Matches
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 <cstddef>
10#include <type_traits>
11#include <utility>
12
13#include <Kokkos_Core.hpp>
14
15#include "detail/macros.hpp"
16
17#include "chunk_traits.hpp"
18#include "discrete_domain.hpp"
19
20namespace ddc {
21
22/** Access the domain (or subdomain) of a view
23 * @param[in] chunk the view whose domain to access
24 * @return the domain of view in the queried dimensions
25 */
26template <class... QueryDDims, class ChunkType>
27KOKKOS_FUNCTION auto get_domain(ChunkType const& chunk) noexcept
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>
34class ChunkCommon
35{
36public:
37 using discrete_domain_type = SupportType;
38
39 /// The dereferenceable part of the co-domain but with a different domain, starting at 0
40 using allocation_mdspan_type = Kokkos::mdspan<
41 ElementType,
42 Kokkos::dextents<std::size_t, SupportType::rank()>,
43 LayoutStridedPolicy>;
44
45 using const_allocation_mdspan_type = Kokkos::mdspan<
46 ElementType const,
47 Kokkos::dextents<std::size_t, SupportType::rank()>,
48 LayoutStridedPolicy>;
49
50 using discrete_element_type = typename discrete_domain_type::discrete_element_type;
51
52 using discrete_vector_type = typename discrete_domain_type::discrete_vector_type;
53
54 using extents_type = typename allocation_mdspan_type::extents_type;
55
56 using layout_type = typename allocation_mdspan_type::layout_type;
57
58 using accessor_type = typename allocation_mdspan_type::accessor_type;
59
60 using mapping_type = typename allocation_mdspan_type::mapping_type;
61
62 using element_type = typename allocation_mdspan_type::element_type;
63
64 using value_type = typename allocation_mdspan_type::value_type;
65
66 using size_type = typename allocation_mdspan_type::size_type;
67
68 using data_handle_type = typename allocation_mdspan_type::data_handle_type;
69
70 using reference = typename allocation_mdspan_type::reference;
71
72 // ChunkCommon, ChunkSpan and Chunk need to access to m_allocation_mdspan_mdspan and m_domain of other template versions
73 template <class, class, class>
74 friend class ChunkCommon;
75
76 template <class, class, class, class>
77 friend class ChunkSpan;
78
79 template <class, class, class>
80 friend class Chunk;
81
82 static_assert(mapping_type::is_always_strided());
83
84protected:
85 /// The raw view of the data
86 allocation_mdspan_type m_allocation_mdspan;
87
88 /// The mesh on which this chunk is defined
89 SupportType m_domain;
90
91public:
92 static KOKKOS_FUNCTION constexpr int rank() noexcept
93 {
94 return extents_type::rank();
95 }
96
97 static KOKKOS_FUNCTION constexpr int rank_dynamic() noexcept
98 {
99 return extents_type::rank_dynamic();
100 }
101
102 static KOKKOS_FUNCTION constexpr size_type static_extent(std::size_t r) noexcept
103 {
104 return extents_type::static_extent(r);
105 }
106
107 static KOKKOS_FUNCTION constexpr bool is_always_unique() noexcept
108 {
109 return mapping_type::is_always_unique();
110 }
111
112 static KOKKOS_FUNCTION constexpr bool is_always_exhaustive() noexcept
113 {
114 return mapping_type::is_always_exhaustive();
115 }
116
117 static KOKKOS_FUNCTION constexpr bool is_always_strided() noexcept
118 {
119 return mapping_type::is_always_strided();
120 }
121
122private:
123 template <class Mapping = mapping_type>
124 static KOKKOS_FUNCTION constexpr std::
125 enable_if_t<std::is_constructible_v<Mapping, extents_type>, allocation_mdspan_type>
126 make_allocation_mdspan(ElementType* ptr, SupportType const& domain)
127 {
128 return allocation_mdspan_type(ptr, detail::array(domain.extents()));
129 }
130
131public:
132 KOKKOS_FUNCTION constexpr accessor_type accessor() const
133 {
134 return m_allocation_mdspan.accessor();
135 }
136
137 KOKKOS_FUNCTION constexpr typename SupportType::discrete_vector_type extents() const noexcept
138 {
139 return m_domain.extents();
140 }
141
142 template <class QueryDDim>
143 KOKKOS_FUNCTION constexpr size_type extent() const noexcept
144 {
145 return m_domain.template extent<QueryDDim>();
146 }
147
148 KOKKOS_FUNCTION constexpr size_type size() const noexcept
149 {
150 return allocation_mdspan().size();
151 }
152
153 KOKKOS_FUNCTION constexpr mapping_type mapping() const noexcept
154 {
155 return allocation_mdspan().mapping();
156 }
157
158 KOKKOS_FUNCTION constexpr bool is_unique() const noexcept
159 {
160 return allocation_mdspan().is_unique();
161 }
162
163 KOKKOS_FUNCTION constexpr bool is_exhaustive() const noexcept
164 {
165 return allocation_mdspan().is_exhaustive();
166 }
167
168 KOKKOS_FUNCTION constexpr bool is_strided() const noexcept
169 {
170 return allocation_mdspan().is_strided();
171 }
172
173 template <class QueryDDim>
174 KOKKOS_FUNCTION constexpr size_type stride() const
175 {
176 return m_allocation_mdspan.stride(type_seq_rank_v<QueryDDim, to_type_seq_t<SupportType>>);
177 }
178
179 /** Provide access to the domain on which this chunk is defined
180 * @return the domain on which this chunk is defined
181 */
182 KOKKOS_FUNCTION constexpr SupportType domain() const noexcept
183 {
184 return m_domain;
185 }
186
187 /** Provide access to the domain on which this chunk is defined
188 * @return the domain on which this chunk is defined
189 */
190 template <class... QueryDDims>
191 KOKKOS_FUNCTION constexpr DiscreteDomain<QueryDDims...> domain() const noexcept
192 {
193 return DiscreteDomain<QueryDDims...>(domain());
194 }
195
196protected:
197 /// Empty ChunkCommon
198 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon() = default;
199
200 /** Constructs a new ChunkCommon from scratch
201 * @param allocation_mdspan
202 * @param domain
203 */
205 allocation_mdspan_type allocation_mdspan,
206 SupportType const& domain) noexcept
207 : m_allocation_mdspan(std::move(allocation_mdspan))
208 , m_domain(domain)
209 {
210 }
211
212 /** Constructs a new ChunkCommon from scratch
213 * @param ptr the allocation pointer to the data
214 * @param domain the domain that sustains the view
215 */
216 template <
217 class Mapping = mapping_type,
218 std::enable_if_t<std::is_constructible_v<Mapping, extents_type>, int> = 0>
219 KOKKOS_FUNCTION constexpr ChunkCommon(ElementType* ptr, SupportType const& domain)
220 : m_allocation_mdspan(make_allocation_mdspan(ptr, domain))
221 , m_domain(domain)
222 {
223 // Handle the case where an allocation of size 0 returns a nullptr.
224 assert(domain.empty() || ((ptr != nullptr) && !domain.empty()));
225 }
226
227 /** Constructs a new ChunkCommon by copy, yields a new view to the same data
228 * @param other the ChunkCommon to copy
229 */
230 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon const& other) = default;
231
232 /** Constructs a new ChunkCommon by move
233 * @param other the ChunkCommon to move
234 */
235 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon&& other) noexcept = default;
236
237 KOKKOS_DEFAULTED_FUNCTION ~ChunkCommon() noexcept = default;
238
239 /** Copy-assigns a new value to this ChunkCommon, yields a new view to the same data
240 * @param other the ChunkCommon to copy
241 * @return *this
242 */
243 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon& operator=(ChunkCommon const& other) = default;
244
245 /** Move-assigns a new value to this ChunkCommon
246 * @param other the ChunkCommon to move
247 * @return *this
248 */
249 KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon& operator=(ChunkCommon&& other) noexcept
250 = default;
251
252 /** Access to the underlying allocation pointer
253 * @return allocation pointer
254 */
255 KOKKOS_FUNCTION constexpr ElementType* data_handle() const
256 {
257 return m_allocation_mdspan.data_handle();
258 }
259
260 /** Provide a modifiable view of the data
261 * @return a modifiable view of the data
262 */
263 KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
264 {
265 return m_allocation_mdspan;
266 }
267};
268
269} // namespace ddc
friend class ChunkSpan
KOKKOS_FUNCTION constexpr bool is_strided() const noexcept
KOKKOS_DEFAULTED_FUNCTION ~ChunkCommon() noexcept=default
KOKKOS_FUNCTION constexpr ElementType * data_handle() const
Access to the underlying allocation pointer.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon()=default
Empty ChunkCommon.
allocation_mdspan_type m_allocation_mdspan
The raw view of the data.
KOKKOS_FUNCTION constexpr SupportType domain() const noexcept
Provide access to the domain on which this chunk is defined.
static KOKKOS_FUNCTION constexpr bool is_always_strided() noexcept
KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
Provide a modifiable view of the data.
KOKKOS_FUNCTION constexpr ChunkCommon(allocation_mdspan_type allocation_mdspan, SupportType const &domain) noexcept
Constructs a new ChunkCommon from scratch.
KOKKOS_FUNCTION constexpr ChunkCommon(ElementType *ptr, SupportType const &domain)
Constructs a new ChunkCommon from scratch.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon const &other)=default
Constructs a new ChunkCommon by copy, yields a new view to the same data.
static KOKKOS_FUNCTION constexpr bool is_always_unique() noexcept
KOKKOS_FUNCTION constexpr DiscreteDomain< QueryDDims... > domain() const noexcept
Provide access to the domain on which this chunk is defined.
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 &&other) noexcept=default
Constructs a new ChunkCommon by move.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon & operator=(ChunkCommon &&other) noexcept=default
Move-assigns a new value to this ChunkCommon.
KOKKOS_FUNCTION constexpr size_type extent() const noexcept
KOKKOS_FUNCTION constexpr size_type stride() const
KOKKOS_FUNCTION constexpr bool is_exhaustive() const noexcept
KOKKOS_FUNCTION constexpr mapping_type mapping() const noexcept
static KOKKOS_FUNCTION constexpr int rank() noexcept
KOKKOS_FUNCTION constexpr bool is_unique() const noexcept
KOKKOS_FUNCTION constexpr accessor_type accessor() const
KOKKOS_FUNCTION constexpr SupportType::discrete_vector_type extents() const noexcept
KOKKOS_FUNCTION constexpr size_type size() const noexcept
SupportType m_domain
The mesh on which this chunk is defined.
static KOKKOS_FUNCTION constexpr int rank_dynamic() noexcept
static KOKKOS_FUNCTION constexpr size_type static_extent(std::size_t r) noexcept
static KOKKOS_FUNCTION constexpr bool is_always_exhaustive() noexcept
friend class Chunk
Definition chunk.hpp:81
friend class DiscreteDomain
The top-level namespace of DDC.
KOKKOS_FUNCTION auto get_domain(ChunkType const &chunk) noexcept
Access the domain (or subdomain) of a view.