10#include <experimental/mdspan>
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"
20template <
class ElementType,
class,
class Allocator = HostAllocator<ElementType>>
26template <
class ElementType,
class... DDims,
class Allocator>
35 using internal_mdspan_type =
typename base_type::internal_mdspan_type;
42 std::experimental::layout_right,
43 typename Allocator::memory_space>;
49 std::experimental::layout_right,
50 typename Allocator::memory_space>;
53 using allocation_mdspan_type =
typename base_type::allocation_mdspan_type;
55 using const_allocation_mdspan_type =
typename base_type::const_allocation_mdspan_type;
57 using mdomain_type =
typename base_type::mdomain_type;
59 using memory_space =
typename Allocator::memory_space;
61 using discrete_element_type =
typename base_type::discrete_element_type;
63 using extents_type =
typename base_type::extents_type;
65 using layout_type =
typename base_type::layout_type;
67 using mapping_type =
typename base_type::mapping_type;
69 using element_type =
typename base_type::element_type;
71 using value_type =
typename base_type::value_type;
73 using size_type =
typename base_type::size_type;
75 using data_handle_type =
typename base_type::data_handle_type;
77 using reference =
typename base_type::reference;
79 template <
class,
class,
class>
83 Allocator m_allocator;
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))
103 explicit Chunk(mdomain_type
const& domain, Allocator allocator = Allocator())
104 :
Chunk(
"no-label", domain, std::move(allocator))
112
113
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))
119 other.m_internal_mdspan = internal_mdspan_type(
nullptr, other.m_internal_mdspan.mapping());
124 if (
this->m_internal_mdspan.data_handle()) {
125 m_allocator.deallocate(
this->data_handle(),
this->size());
133
134
135
138 if (
this == &other) {
141 if (
this->m_internal_mdspan.data_handle()) {
142 m_allocator.deallocate(
this->data_handle(),
this->size());
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());
153 template <
class... QueryDDims>
154 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
const
156 return view_type(*
this)[slice_spec];
160 template <
class... QueryDDims>
161 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
163 return span_view()[slice_spec];
167 template <
class... QueryDDims>
170 return span_view()[odomain];
174 template <
class... QueryDDims>
177 return span_view()[odomain];
181
182
183
184 template <
class... DElems>
185 element_type
const&
operator()(DElems
const&... delems)
const noexcept
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...))...);
197
198
199
200 template <
class... DElems>
201 element_type&
operator()(DElems
const&... delems)
noexcept
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...))...);
213
214
215 char const*
label()
const
217 return m_label.c_str();
221
222
225 return base_type::data_handle();
229
230
233 return base_type::data_handle();
237
238
241 return base_type::allocation_mdspan();
245
246
249 return base_type::allocation_mdspan();
253
254
257 auto s =
this->allocation_mdspan();
258 auto kokkos_layout = detail::build_kokkos_layout(
261 std::make_index_sequence<
sizeof...(DDims)> {});
263 detail::mdspan_to_kokkos_element_t<ElementType,
sizeof...(DDims)>,
264 decltype(kokkos_layout),
265 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
269
270
273 auto s =
this->allocation_mdspan();
274 auto kokkos_layout = detail::build_kokkos_layout(
277 std::make_index_sequence<
sizeof...(DDims)> {});
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);
286 return view_type(*
this);
291 return view_type(*
this);
296 return span_type(*
this);
300template <
class... DDims,
class Allocator>
304template <
class... DDims,
class Allocator>
friend class ChunkSpan
Definition: chunk_common.hpp:85
friend class ChunkCommon
Definition: chunk_common.hpp:82
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
~Chunk()
Definition: chunk.hpp:122
ElementType const * data_handle() const
Access to the underlying allocation pointer.
Definition: chunk.hpp:223
friend class Chunk
Definition: chunk.hpp:80
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
span_type span_view()
Definition: chunk.hpp:294
Chunk(Chunk const &other)=delete
Deleted: use deepcopy instead.
Chunk(Chunk &&other)
Constructs a new Chunk by move.
Definition: chunk.hpp:114
Chunk()=default
Empty Chunk.
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 >