12#include <Kokkos_Core.hpp>
14#include "detail/kokkos.hpp"
15#include "detail/type_traits.hpp"
17#include "chunk_common.hpp"
18#include "chunk_span.hpp"
19#include "chunk_traits.hpp"
20#include "kokkos_allocator.hpp"
24template <
class ElementType,
class,
class Allocator = HostAllocator<ElementType>>
30template <
class ElementType,
class SupportType,
class Allocator>
31class Chunk :
public ChunkCommon<ElementType, SupportType, Kokkos::layout_right>
34 using base_type =
ChunkCommon<ElementType, SupportType, Kokkos::layout_right>;
42 typename Allocator::memory_space>;
49 typename Allocator::memory_space>;
52 using allocation_mdspan_type =
typename base_type::allocation_mdspan_type;
54 using const_allocation_mdspan_type =
typename base_type::const_allocation_mdspan_type;
56 using discrete_domain_type =
typename base_type::discrete_domain_type;
58 using memory_space =
typename Allocator::memory_space;
60 using discrete_element_type =
typename base_type::discrete_element_type;
62 using discrete_vector_type =
typename base_type::discrete_vector_type;
64 using extents_type =
typename base_type::extents_type;
66 using layout_type =
typename base_type::layout_type;
68 using mapping_type =
typename base_type::mapping_type;
70 using element_type =
typename base_type::element_type;
72 using value_type =
typename base_type::value_type;
74 using size_type =
typename base_type::size_type;
76 using data_handle_type =
typename base_type::data_handle_type;
78 using reference =
typename base_type::reference;
80 template <
class,
class,
class>
84 Allocator m_allocator;
94 std::string
const& label,
95 SupportType
const& domain,
96 Allocator allocator = Allocator())
97 : base_type(allocator.allocate(label, domain.size()), domain)
98 , m_allocator(std::move(allocator))
104 explicit Chunk(SupportType
const& domain, Allocator allocator = Allocator())
105 :
Chunk(
"no-label", domain, std::move(allocator))
113
114
116 : base_type(std::move(
static_cast<base_type&>(other)))
117 , m_allocator(std::move(other.m_allocator))
118 , m_label(std::move(other.m_label))
120 other.m_allocation_mdspan
121 = allocation_mdspan_type(
nullptr, other.m_allocation_mdspan.mapping());
126 if (
this->m_allocation_mdspan.data_handle()) {
127 m_allocator.deallocate(
this->data_handle(),
this->size());
135
136
137
140 if (
this == &other) {
143 if (
this->m_allocation_mdspan.data_handle()) {
144 m_allocator.deallocate(
this->data_handle(),
this->size());
146 static_cast<base_type&>(*
this) = std::move(
static_cast<base_type&>(other));
147 m_allocator = std::move(other.m_allocator);
148 m_label = std::move(other.m_label);
149 other.m_allocation_mdspan
150 = allocation_mdspan_type(
nullptr, other.m_allocation_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
189 std::enable_if_t<detail::all_of_v<is_discrete_element_v<DElems>...>,
int> = 0>
190 element_type
const&
operator()(DElems
const&... delems)
const noexcept
193 SupportType::rank() == (0 + ... + DElems::size()),
194 "Invalid number of dimensions");
195 assert(
this->m_domain.contains(delems...));
196 return DDC_MDSPAN_ACCESS_OP(
197 this->m_allocation_mdspan,
198 detail::array(
this->m_domain.distance_from_front(delems...)));
202
203
204
207 std::enable_if_t<detail::all_of_v<is_discrete_vector_v<DVects>...>,
int> = 0,
208 std::enable_if_t<
sizeof...(DVects) != 0,
int> = 0>
209 element_type
const&
operator()(DVects
const&... dvects)
const noexcept
212 SupportType::rank() == (0 + ... + DVects::size()),
213 "Invalid number of dimensions");
214 return DDC_MDSPAN_ACCESS_OP(
215 this->m_allocation_mdspan,
216 detail::array(discrete_vector_type(dvects...)));
220
221
222
225 std::enable_if_t<detail::all_of_v<is_discrete_element_v<DElems>...>,
int> = 0>
226 element_type&
operator()(DElems
const&... delems)
noexcept
229 SupportType::rank() == (0 + ... + DElems::size()),
230 "Invalid number of dimensions");
231 assert(
this->m_domain.contains(delems...));
232 return DDC_MDSPAN_ACCESS_OP(
233 this->m_allocation_mdspan,
234 detail::array(
this->m_domain.distance_from_front(delems...)));
238
239
240
243 std::enable_if_t<detail::all_of_v<is_discrete_vector_v<DVects>...>,
int> = 0,
244 std::enable_if_t<
sizeof...(DVects) != 0,
int> = 0>
245 element_type&
operator()(DVects
const&... dvects)
noexcept
248 SupportType::rank() == (0 + ... + DVects::size()),
249 "Invalid number of dimensions");
250 return DDC_MDSPAN_ACCESS_OP(
251 this->m_allocation_mdspan,
252 detail::array(discrete_vector_type(dvects...)));
256
257
258 char const*
label()
const
260 return m_label.c_str();
264
265
268 return base_type::data_handle();
272
273
276 return base_type::data_handle();
280
281
284 return base_type::allocation_mdspan();
288
289
292 return base_type::allocation_mdspan();
296
297
300 auto s =
this->allocation_mdspan();
301 auto kokkos_layout = detail::build_kokkos_layout(
304 std::make_index_sequence<SupportType::rank()> {});
306 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
307 decltype(kokkos_layout),
308 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
312
313
316 auto s =
this->allocation_mdspan();
317 auto kokkos_layout = detail::build_kokkos_layout(
320 std::make_index_sequence<SupportType::rank()> {});
322 detail::mdspan_to_kokkos_element_t<ElementType
const, SupportType::rank()>,
323 decltype(kokkos_layout),
324 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
329 return view_type(*
this);
334 return view_type(*
this);
339 return span_type(*
this);
343template <
class SupportType,
class Allocator>
344Chunk(std::string
const&, SupportType
const&, Allocator)
347template <
class SupportType,
class Allocator>
348Chunk(SupportType
const&, Allocator)
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
char const * label() const
Returns the label of the Chunk.
auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Slice out some dimensions.
const_allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
element_type & operator()(DVects const &... dvects) noexcept
Element access using a list of DiscreteVector.
ElementType const * data_handle() const
Access to the underlying allocation pointer.
auto allocation_kokkos_view()
Provide an unmanaged Kokkos::View on the memory allocation.
auto allocation_kokkos_view() const
Provide an unmanaged Kokkos::View on the memory allocation.
Chunk(SupportType const &domain, Allocator allocator=Allocator())
Construct a Chunk on a domain with uninitialized values.
Chunk & operator=(Chunk const &other)=delete
Deleted: use deepcopy instead.
allocation_mdspan_type allocation_mdspan()
Provide a mdspan on the memory allocation.
ElementType * data_handle()
Access to the underlying allocation pointer.
element_type const & operator()(DVects const &... dvects) const noexcept
Element access using a list of DiscreteVector.
view_type span_cview() const
Chunk(Chunk const &other)=delete
Deleted: use deepcopy instead.
Chunk(std::string const &label, SupportType const &domain, Allocator allocator=Allocator())
Construct a labeled Chunk on a domain with uninitialized values.
element_type & operator()(DElems const &... delems) noexcept
Element access using a list of DiscreteElement.
Chunk()=default
Empty Chunk.
Chunk & operator=(Chunk &&other) noexcept
Move-assigns a new value to this field.
Chunk(Chunk &&other) noexcept
Constructs a new Chunk by move.
view_type span_view() const
element_type const & operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
auto operator[](DiscreteDomain< QueryDDims... > const &odomain)
Slice out some dimensions.
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
friend class DiscreteDomain
The top-level namespace of DDC.
constexpr bool enable_chunk< Chunk< ElementType, SupportType, Allocator > >
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 >