DDC 0.16.0
Loading...
Searching...
No Matches
chunk.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 <cassert>
8#include <string>
9#include <utility>
10
11#include <Kokkos_Core.hpp>
12
13#include "detail/kokkos.hpp"
14
15#include "chunk_common.hpp"
16#include "chunk_span.hpp"
17#include "chunk_traits.hpp"
23
24namespace ddc {
25
26template <class ElementType, class, class Allocator = HostAllocator<ElementType>>
27class Chunk;
28
29template <class ElementType, class SupportType, class Allocator>
30inline constexpr bool enable_chunk<Chunk<ElementType, SupportType, Allocator>> = true;
31
32template <class ElementType, class SupportType, class Allocator>
33class Chunk : public ChunkCommon<ElementType, SupportType, Kokkos::layout_right>
34{
35protected:
36 using base_type = ChunkCommon<ElementType, SupportType, Kokkos::layout_right>;
37
38public:
39 /// type of a span of this full chunk
40 using span_type = ChunkSpan<
41 ElementType,
42 SupportType,
43 Kokkos::layout_right,
44 typename Allocator::memory_space>;
45
46 /// type of a view of this full chunk
47 using view_type = ChunkSpan<
48 ElementType const,
49 SupportType,
50 Kokkos::layout_right,
51 typename Allocator::memory_space>;
52
53 /// The dereferenceable part of the co-domain but with indexing starting at 0
54 using allocation_mdspan_type = base_type::allocation_mdspan_type;
55
56 using const_allocation_mdspan_type = base_type::const_allocation_mdspan_type;
57
58 using discrete_domain_type = base_type::discrete_domain_type;
59
60 using memory_space = Allocator::memory_space;
61
62 using discrete_element_type = base_type::discrete_element_type;
63
64 using discrete_vector_type = base_type::discrete_vector_type;
65
66 using extents_type = base_type::extents_type;
67
68 using layout_type = base_type::layout_type;
69
70 using mapping_type = base_type::mapping_type;
71
72 using element_type = base_type::element_type;
73
74 using value_type = base_type::value_type;
75
76 using size_type = base_type::size_type;
77
78 using data_handle_type = base_type::data_handle_type;
79
80 using reference = base_type::reference;
81
82 template <class, class, class>
83 friend class Chunk;
84
85private:
86 Allocator m_allocator;
87
88 std::string m_label;
89
90public:
91 /// Empty Chunk
92 Chunk() = default;
93
94 /// Construct a labeled Chunk on a domain with uninitialized values
95 explicit Chunk(
96 std::string const& label,
97 SupportType const& domain,
98 Allocator allocator = Allocator())
99 : base_type(allocator.allocate(label, domain.size()), domain)
100 , m_allocator(std::move(allocator))
101 , m_label(label)
102 {
103 }
104
105 /// Construct a Chunk on a domain with uninitialized values
106 explicit Chunk(SupportType const& domain, Allocator allocator = Allocator())
107 : Chunk("no-label", domain, std::move(allocator))
108 {
109 }
110
111 /// Deleted: use deepcopy instead
112 Chunk(Chunk const& other) = delete;
113
114 /** Constructs a new Chunk by move
115 * @param other the Chunk to move
116 */
117 Chunk(Chunk&& other) noexcept
118 : base_type(std::move(static_cast<base_type&>(other)))
119 , m_allocator(std::move(other.m_allocator))
120 , m_label(std::move(other.m_label))
121 {
122 other.m_allocation_mdspan
123 = allocation_mdspan_type(nullptr, other.m_allocation_mdspan.mapping());
124 }
125
126 ~Chunk() noexcept
127 {
128 if (this->m_allocation_mdspan.data_handle()) {
129 m_allocator.deallocate(this->data_handle(), this->size());
130 }
131 }
132
133 /// Deleted: use deepcopy instead
134 Chunk& operator=(Chunk const& other) = delete;
135
136 /** Move-assigns a new value to this field
137 * @param other the Chunk to move
138 * @return *this
139 */
140 Chunk& operator=(Chunk&& other) noexcept
141 {
142 if (this->data_handle()) {
143 m_allocator.deallocate(this->data_handle(), this->size());
144 }
145 static_cast<base_type&>(*this) = std::move(static_cast<base_type&>(other));
146 m_allocator = std::move(other.m_allocator);
147 m_label = std::move(other.m_label);
148 other.m_allocation_mdspan
149 = allocation_mdspan_type(nullptr, other.m_allocation_mdspan.mapping());
150
151 return *this;
152 }
153
154 /// Slice out some dimensions
155 template <concepts::discrete_dimension... QueryDDims>
156 auto operator[](DiscreteVector<QueryDDims...> const& slice_spec) const
157 {
158 return view_type(*this)[slice_spec];
159 }
160
161 /// Slice out some dimensions
162 template <concepts::discrete_dimension... QueryDDims>
163 auto operator[](DiscreteVector<QueryDDims...> const& slice_spec)
164 {
165 return span_view()[slice_spec];
166 }
167
168 /// Slice out some dimensions
169 template <concepts::discrete_dimension... QueryDDims>
170 auto operator[](DiscreteElement<QueryDDims...> const& slice_spec) const
171 {
172 return view_type(*this)[slice_spec];
173 }
174
175 /// Slice out some dimensions
176 template <concepts::discrete_dimension... QueryDDims>
177 auto operator[](DiscreteElement<QueryDDims...> const& slice_spec)
178 {
179 return span_view()[slice_spec];
180 }
181
182 /// Slice out some dimensions
183 template <concepts::discrete_dimension... QueryDDims>
184 auto operator[](DiscreteDomain<QueryDDims...> const& odomain) const
185 requires(is_discrete_domain_v<SupportType>)
186 {
187 return span_view()[odomain];
188 }
189
190 /// Slice out some dimensions
191 template <concepts::discrete_dimension... QueryDDims>
192 auto operator[](DiscreteDomain<QueryDDims...> const& odomain)
193 requires(is_discrete_domain_v<SupportType>)
194 {
195 return span_view()[odomain];
196 }
197
198 /** Element access using a list of DiscreteElement
199 * @param delems discrete coordinates
200 * @return const-reference to this element
201 */
202 template <concepts::discrete_element... DElems>
203 element_type const& operator()(DElems const&... delems) const noexcept
204 {
205 static_assert(
206 SupportType::rank() == (0 + ... + DElems::size()),
207 "Invalid number of dimensions");
208 assert(this->m_domain.contains(delems...));
209 return DDC_MDSPAN_ACCESS_OP(
210 this->m_allocation_mdspan,
211 detail::array(this->m_domain.distance_from_front(delems...)));
212 }
213
214 /** Element access using a list of DiscreteVector
215 * @param dvects discrete vectors
216 * @return reference to this element
217 */
218 template <concepts::discrete_vector... DVects>
219 element_type const& operator()(DVects const&... dvects) const noexcept
220 requires(sizeof...(DVects) != 0)
221 {
222 static_assert(
223 SupportType::rank() == (0 + ... + DVects::size()),
224 "Invalid number of dimensions");
225 return DDC_MDSPAN_ACCESS_OP(
226 this->m_allocation_mdspan,
227 detail::array(discrete_vector_type(dvects...)));
228 }
229
230 /** Element access using a list of DiscreteElement
231 * @param delems discrete coordinates
232 * @return reference to this element
233 */
234 template <concepts::discrete_element... DElems>
235 element_type& operator()(DElems const&... delems) noexcept
236 {
237 static_assert(
238 SupportType::rank() == (0 + ... + DElems::size()),
239 "Invalid number of dimensions");
240 assert(this->m_domain.contains(delems...));
241 return DDC_MDSPAN_ACCESS_OP(
242 this->m_allocation_mdspan,
243 detail::array(this->m_domain.distance_from_front(delems...)));
244 }
245
246 /** Element access using a list of DiscreteVector
247 * @param dvects discrete vectors
248 * @return reference to this element
249 */
250 template <concepts::discrete_vector... DVects>
251 element_type& operator()(DVects const&... dvects) noexcept
252 requires(sizeof...(DVects) != 0)
253 {
254 static_assert(
255 SupportType::rank() == (0 + ... + DVects::size()),
256 "Invalid number of dimensions");
257 return DDC_MDSPAN_ACCESS_OP(
258 this->m_allocation_mdspan,
259 detail::array(discrete_vector_type(dvects...)));
260 }
261
262 /** Returns the label of the Chunk
263 * @return c-string
264 */
265 char const* label() const
266 {
267 return m_label.c_str();
268 }
269
270 /** Access to the underlying allocation pointer
271 * @return read-only allocation pointer
272 */
273 ElementType const* data_handle() const
274 {
275 return base_type::data_handle();
276 }
277
278 /** Access to the underlying allocation pointer
279 * @return allocation pointer
280 */
281 ElementType* data_handle()
282 {
283 return base_type::data_handle();
284 }
285
286 /** Provide a mdspan on the memory allocation
287 * @return read-only allocation mdspan
288 */
289 const_allocation_mdspan_type allocation_mdspan() const
290 {
291 return base_type::allocation_mdspan();
292 }
293
294 /** Provide a mdspan on the memory allocation
295 * @return allocation mdspan
296 */
297 allocation_mdspan_type allocation_mdspan()
298 {
299 return base_type::allocation_mdspan();
300 }
301
302 /** Provide an unmanaged `Kokkos::View` on the memory allocation
303 * @return allocation `Kokkos::View`
304 */
306 {
307 auto s = this->allocation_mdspan();
308 auto kokkos_layout = detail::build_kokkos_layout(
309 s.extents(),
310 s.mapping(),
311 std::make_index_sequence<SupportType::rank()> {});
312 return Kokkos::View<
313 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
314 decltype(kokkos_layout),
315 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
316 }
317
318 /** Provide an unmanaged `Kokkos::View` on the memory allocation
319 * @return read-only allocation `Kokkos::View`
320 */
321 auto allocation_kokkos_view() const
322 {
323 auto s = this->allocation_mdspan();
324 auto kokkos_layout = detail::build_kokkos_layout(
325 s.extents(),
326 s.mapping(),
327 std::make_index_sequence<SupportType::rank()> {});
328 return Kokkos::View<
329 detail::mdspan_to_kokkos_element_t<ElementType const, SupportType::rank()>,
330 decltype(kokkos_layout),
331 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
332 }
333
334 view_type span_cview() const
335 {
336 return view_type(*this);
337 }
338
339 view_type span_view() const
340 {
341 return view_type(*this);
342 }
343
344 span_type span_view()
345 {
346 return span_type(*this);
347 }
348};
349
350template <class SupportType, class Allocator>
351Chunk(std::string const&, SupportType const&, Allocator)
352 -> Chunk<typename Allocator::value_type, SupportType, Allocator>;
353
354template <class SupportType, class Allocator>
355Chunk(SupportType const&, Allocator)
356 -> Chunk<typename Allocator::value_type, SupportType, Allocator>;
357
358} // namespace ddc
friend class ChunkSpan
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
Definition chunk.hpp:170
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
Definition chunk.hpp:156
char const * label() const
Returns the label of the Chunk.
Definition chunk.hpp:265
const_allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
Definition chunk.hpp:289
ElementType const * data_handle() const
Access to the underlying allocation pointer.
Definition chunk.hpp:273
auto allocation_kokkos_view()
Provide an unmanaged Kokkos::View on the memory allocation.
Definition chunk.hpp:305
auto operator[](DiscreteDomain< QueryDDims... > const &odomain)
Slice out some dimensions.
Definition chunk.hpp:192
auto allocation_kokkos_view() const
Provide an unmanaged Kokkos::View on the memory allocation.
Definition chunk.hpp:321
Chunk(SupportType const &domain, Allocator allocator=Allocator())
Construct a Chunk on a domain with uninitialized values.
Definition chunk.hpp:106
Chunk & operator=(Chunk const &other)=delete
Deleted: use deepcopy instead.
allocation_mdspan_type allocation_mdspan()
Provide a mdspan on the memory allocation.
Definition chunk.hpp:297
ElementType * data_handle()
Access to the underlying allocation pointer.
Definition chunk.hpp:281
view_type span_cview() const
Definition chunk.hpp:334
Chunk(Chunk const &other)=delete
Deleted: use deepcopy instead.
~Chunk() noexcept
Definition chunk.hpp:126
Chunk(std::string const &label, SupportType const &domain, Allocator allocator=Allocator())
Construct a labeled Chunk on a domain with uninitialized values.
Definition chunk.hpp:95
element_type & operator()(DElems const &... delems) noexcept
Element access using a list of DiscreteElement.
Definition chunk.hpp:235
Chunk()=default
Empty Chunk.
Chunk & operator=(Chunk &&other) noexcept
Move-assigns a new value to this field.
Definition chunk.hpp:140
Chunk(Chunk &&other) noexcept
Constructs a new Chunk by move.
Definition chunk.hpp:117
friend class Chunk
Definition chunk.hpp:83
span_type span_view()
Definition chunk.hpp:344
view_type span_view() const
Definition chunk.hpp:339
auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Slice out some dimensions.
Definition chunk.hpp:184
element_type const & operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
Definition chunk.hpp:203
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition chunk.hpp:163
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition chunk.hpp:177
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
The top-level namespace of DDC.
constexpr bool enable_chunk< Chunk< ElementType, SupportType, Allocator > >
Definition chunk.hpp:30
Chunk(SupportType const &, Allocator) -> Chunk< typename Allocator::value_type, SupportType, Allocator >
Chunk(std::string const &, SupportType const &, Allocator) -> Chunk< typename Allocator::value_type, SupportType, Allocator >