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