11#include <Kokkos_Core.hpp>
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"
21template <
class ElementType,
class,
class Allocator = HostAllocator<ElementType>>
27template <
class ElementType,
class... DDims,
class Allocator>
35 using internal_mdspan_type =
typename base_type::internal_mdspan_type;
43 typename Allocator::memory_space>;
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 discrete_domain_type =
typename base_type::discrete_domain_type;
59 using mdomain_type [[deprecated(
"Use `discrete_domain_type` instead")]] =
60 typename base_type::mdomain_type;
62 using memory_space =
typename Allocator::memory_space;
64 using discrete_element_type =
typename base_type::discrete_element_type;
66 using extents_type =
typename base_type::extents_type;
68 using layout_type =
typename base_type::layout_type;
70 using mapping_type =
typename base_type::mapping_type;
72 using element_type =
typename base_type::element_type;
74 using value_type =
typename base_type::value_type;
76 using size_type =
typename base_type::size_type;
78 using data_handle_type =
typename base_type::data_handle_type;
80 using reference =
typename base_type::reference;
82 template <
class,
class,
class>
86 Allocator m_allocator;
96 std::string
const& label,
97 discrete_domain_type
const& domain,
98 Allocator allocator = Allocator())
99 : base_type(allocator.allocate(label, domain.size()), domain)
100 , m_allocator(std::move(allocator))
106 explicit Chunk(discrete_domain_type
const& domain, Allocator allocator = Allocator())
107 :
Chunk(
"no-label", domain, std::move(allocator))
115
116
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))
122 other.m_internal_mdspan = internal_mdspan_type(
nullptr, other.m_internal_mdspan.mapping());
127 if (
this->m_internal_mdspan.data_handle()) {
128 m_allocator.deallocate(
this->data_handle(),
this->size());
136
137
138
141 if (
this == &other) {
144 if (
this->m_internal_mdspan.data_handle()) {
145 m_allocator.deallocate(
this->data_handle(),
this->size());
147 static_cast<base_type&>(*
this) = std::move(
static_cast<base_type&>(other));
148 m_allocator = std::move(other.m_allocator);
149 m_label = std::move(other.m_label);
150 other.m_internal_mdspan = internal_mdspan_type(
nullptr, other.m_internal_mdspan.mapping());
156 template <
class... QueryDDims>
157 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
const
159 return view_type(*
this)[slice_spec];
163 template <
class... QueryDDims>
164 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
166 return span_view()[slice_spec];
170 template <
class... QueryDDims>
173 return span_view()[odomain];
177 template <
class... QueryDDims>
180 return span_view()[odomain];
184
185
186
187 template <
class... DElems>
188 element_type
const&
operator()(DElems
const&... delems)
const noexcept
191 sizeof...(DDims) == (0 + ... + DElems::size()),
192 "Invalid number of dimensions");
193 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
194 assert(((select<DDims>(take<DDims>(delems...)) >= front<DDims>(
this->m_domain)) && ...));
195 assert(((select<DDims>(take<DDims>(delems...)) <= back<DDims>(
this->m_domain)) && ...));
196 return DDC_MDSPAN_ACCESS_OP(
this->m_internal_mdspan, uid<DDims>(take<DDims>(delems...))...);
200
201
202
203 template <
class... DElems>
204 element_type&
operator()(DElems
const&... delems)
noexcept
207 sizeof...(DDims) == (0 + ... + DElems::size()),
208 "Invalid number of dimensions");
209 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
210 assert(((select<DDims>(take<DDims>(delems...)) >= front<DDims>(
this->m_domain)) && ...));
211 assert(((select<DDims>(take<DDims>(delems...)) <= back<DDims>(
this->m_domain)) && ...));
212 return DDC_MDSPAN_ACCESS_OP(
this->m_internal_mdspan, uid<DDims>(take<DDims>(delems...))...);
216
217
218 char const*
label()
const
220 return m_label.c_str();
224
225
228 return base_type::data_handle();
232
233
236 return base_type::data_handle();
240
241
244 return base_type::allocation_mdspan();
248
249
252 return base_type::allocation_mdspan();
256
257
260 auto s =
this->allocation_mdspan();
261 auto kokkos_layout = detail::build_kokkos_layout(
264 std::make_index_sequence<
sizeof...(DDims)> {});
266 detail::mdspan_to_kokkos_element_t<ElementType,
sizeof...(DDims)>,
267 decltype(kokkos_layout),
268 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
272
273
276 auto s =
this->allocation_mdspan();
277 auto kokkos_layout = detail::build_kokkos_layout(
280 std::make_index_sequence<
sizeof...(DDims)> {});
282 detail::mdspan_to_kokkos_element_t<
const ElementType,
sizeof...(DDims)>,
283 decltype(kokkos_layout),
284 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
289 return view_type(*
this);
294 return view_type(*
this);
299 return span_type(*
this);
303template <
class... DDims,
class Allocator>
304Chunk(std::string
const&,
308template <
class... DDims,
class Allocator>
auto operator[](DiscreteDomain< QueryDDims... > const &odomain)
Slice out some dimensions.
auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Slice out some dimensions.
Chunk(discrete_domain_type const &domain, Allocator allocator=Allocator())
Construct a Chunk on a domain with uninitialized values.
element_type const & operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
allocation_mdspan_type allocation_mdspan()
Provide a mdspan on the memory allocation.
ElementType * data_handle()
Access to the underlying allocation pointer.
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
Chunk & operator=(Chunk const &other)=delete
Deleted: use deepcopy instead.
const_allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
auto allocation_kokkos_view() const
Provide an unmanaged Kokkos::View on the memory allocation.
ElementType const * data_handle() const
Access to the underlying allocation pointer.
auto allocation_kokkos_view()
Provide an unmanaged Kokkos::View on the memory allocation.
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.
Chunk(Chunk &&other)
Constructs a new Chunk by move.
Chunk()=default
Empty Chunk.
char const * label() const
Returns the label of the Chunk.
view_type span_view() const
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Chunk & operator=(Chunk &&other)
Move-assigns a new value to this field.
view_type span_cview() const
element_type & operator()(DElems const &... delems) noexcept
Element access using a list of DiscreteElement.
friend class DiscreteDomain
The top-level namespace of DDC.
constexpr bool enable_chunk< Chunk< ElementType, SupportType, Allocator > >
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 >