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