DDC 0.0.0

a discrete domain computation library

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 <string>
8#include <utility>
9
10#include <experimental/mdspan>
11
12#include "ddc/chunk_common.hpp"
13#include "ddc/chunk_span.hpp"
14#include "ddc/chunk_traits.hpp"
15#include "ddc/kokkos_allocator.hpp"
16#include "ddc/parallel_deepcopy.hpp"
17
18namespace ddc {
19
20template <class ElementType, class, class Allocator = HostAllocator<ElementType>>
21class Chunk;
22
23template <class ElementType, class SupportType, class Allocator>
24inline constexpr bool enable_chunk<Chunk<ElementType, SupportType, Allocator>> = true;
25
26template <class ElementType, class... DDims, class Allocator>
27class Chunk<ElementType, DiscreteDomain<DDims...>, Allocator>
28 : public ChunkCommon<ElementType, DiscreteDomain<DDims...>, std::experimental::layout_right>
29{
30protected:
31 using base_type
32 = ChunkCommon<ElementType, DiscreteDomain<DDims...>, std::experimental::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 std::experimental::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 std::experimental::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 mdomain_type = typename base_type::mdomain_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 mdomain_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(mdomain_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)
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()
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)
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(((select<DDims>(take<DDims>(delems...)) >= front<DDims>(this->m_domain)) && ...));
192 assert(((select<DDims>(take<DDims>(delems...)) <= back<DDims>(this->m_domain)) && ...));
193 return this->m_internal_mdspan(uid<DDims>(take<DDims>(delems...))...);
194 }
195
196 /** Element access using a list of DiscreteElement
197 * @param delems discrete coordinates
198 * @return reference to this element
199 */
200 template <class... DElems>
201 element_type& operator()(DElems const&... delems) noexcept
202 {
203 static_assert(
204 sizeof...(DDims) == (0 + ... + DElems::size()),
205 "Invalid number of dimensions");
206 static_assert((is_discrete_element_v<DElems> && ...), "Expected DiscreteElements");
207 assert(((select<DDims>(take<DDims>(delems...)) >= front<DDims>(this->m_domain)) && ...));
208 assert(((select<DDims>(take<DDims>(delems...)) <= back<DDims>(this->m_domain)) && ...));
209 return this->m_internal_mdspan(uid<DDims>(take<DDims>(delems...))...);
210 }
211
212 /** Returns the label of the Chunk
213 * @return c-string
214 */
215 char const* label() const
216 {
217 return m_label.c_str();
218 }
219
220 /** Access to the underlying allocation pointer
221 * @return read-only allocation pointer
222 */
223 ElementType const* data_handle() const
224 {
225 return base_type::data_handle();
226 }
227
228 /** Access to the underlying allocation pointer
229 * @return allocation pointer
230 */
231 ElementType* data_handle()
232 {
233 return base_type::data_handle();
234 }
235
236 /** Provide a mdspan on the memory allocation
237 * @return read-only allocation mdspan
238 */
239 const_allocation_mdspan_type allocation_mdspan() const
240 {
241 return base_type::allocation_mdspan();
242 }
243
244 /** Provide a mdspan on the memory allocation
245 * @return allocation mdspan
246 */
247 allocation_mdspan_type allocation_mdspan()
248 {
249 return base_type::allocation_mdspan();
250 }
251
252 /** Provide an unmanaged `Kokkos::View` on the memory allocation
253 * @return allocation `Kokkos::View`
254 */
256 {
257 auto s = this->allocation_mdspan();
258 auto kokkos_layout = detail::build_kokkos_layout(
259 s.extents(),
260 s.mapping(),
261 std::make_index_sequence<sizeof...(DDims)> {});
262 return Kokkos::View<
263 detail::mdspan_to_kokkos_element_t<ElementType, sizeof...(DDims)>,
264 decltype(kokkos_layout),
265 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
266 }
267
268 /** Provide an unmanaged `Kokkos::View` on the memory allocation
269 * @return read-only allocation `Kokkos::View`
270 */
271 auto allocation_kokkos_view() const
272 {
273 auto s = this->allocation_mdspan();
274 auto kokkos_layout = detail::build_kokkos_layout(
275 s.extents(),
276 s.mapping(),
277 std::make_index_sequence<sizeof...(DDims)> {});
278 return Kokkos::View<
279 detail::mdspan_to_kokkos_element_t<const ElementType, sizeof...(DDims)>,
280 decltype(kokkos_layout),
281 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
282 }
283
284 view_type span_cview() const
285 {
286 return view_type(*this);
287 }
288
289 view_type span_view() const
290 {
291 return view_type(*this);
292 }
293
294 span_type span_view()
295 {
296 return span_type(*this);
297 }
298};
299
300template <class... DDims, class Allocator>
301Chunk(std::string const&, DiscreteDomain<DDims...> const&, Allocator)
302 -> Chunk<typename Allocator::value_type, DiscreteDomain<DDims...>, Allocator>;
303
304template <class... DDims, class Allocator>
305Chunk(DiscreteDomain<DDims...> const&, Allocator)
306 -> Chunk<typename Allocator::value_type, DiscreteDomain<DDims...>, Allocator>;
307
308} // 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
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:247
ElementType * data_handle()
Access to the underlying allocation pointer.
Definition: chunk.hpp:231
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:239
auto allocation_kokkos_view() const
Provide an unmanaged Kokkos::View on the memory allocation.
Definition: chunk.hpp:271
ElementType const * data_handle() const
Access to the underlying allocation pointer.
Definition: chunk.hpp:223
Chunk(mdomain_type const &domain, Allocator allocator=Allocator())
Construct a Chunk on a domain with uninitialized values.
Definition: chunk.hpp:103
auto allocation_kokkos_view()
Provide an unmanaged Kokkos::View on the memory allocation.
Definition: chunk.hpp:255
Chunk(Chunk const &other)=delete
Deleted: use deepcopy instead.
Chunk(Chunk &&other)
Constructs a new Chunk by move.
Definition: chunk.hpp:114
char const * label() const
Returns the label of the Chunk.
Definition: chunk.hpp:215
view_type span_view() const
Definition: chunk.hpp:289
Chunk(std::string const &label, mdomain_type const &domain, Allocator allocator=Allocator())
Construct a labeled Chunk on a domain with uninitialized values.
Definition: chunk.hpp:92
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition: chunk.hpp:161
Chunk & operator=(Chunk &&other)
Move-assigns a new value to this field.
Definition: chunk.hpp:136
view_type span_cview() const
Definition: chunk.hpp:284
element_type & operator()(DElems const &... delems) noexcept
Element access using a list of DiscreteElement.
Definition: chunk.hpp:201
friend class DiscreteDomain
Definition: discrete_domain.hpp:53
Definition: aligned_allocator.hpp:11
constexpr bool enable_chunk< Chunk< ElementType, SupportType, Allocator > >
Definition: chunk.hpp:24
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 >