11#include <Kokkos_Core.hpp>
13#include "detail/kokkos.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 = base_type::allocation_mdspan_type;
54 using const_allocation_mdspan_type = base_type::const_allocation_mdspan_type;
56 using discrete_domain_type = base_type::discrete_domain_type;
58 using memory_space = Allocator::memory_space;
60 using discrete_element_type = base_type::discrete_element_type;
62 using discrete_vector_type = base_type::discrete_vector_type;
64 using extents_type = base_type::extents_type;
66 using layout_type = base_type::layout_type;
68 using mapping_type = base_type::mapping_type;
70 using element_type = base_type::element_type;
72 using value_type = base_type::value_type;
74 using size_type = base_type::size_type;
76 using data_handle_type = base_type::data_handle_type;
78 using reference = 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->data_handle()) {
141 m_allocator.deallocate(
this->data_handle(),
this->size());
143 static_cast<base_type&>(*
this) = std::move(
static_cast<base_type&>(other));
144 m_allocator = std::move(other.m_allocator);
145 m_label = std::move(other.m_label);
146 other.m_allocation_mdspan
147 = allocation_mdspan_type(
nullptr, other.m_allocation_mdspan.mapping());
153 template <
class... QueryDDims>
156 return view_type(*
this)[slice_spec];
160 template <
class... QueryDDims>
163 return span_view()[slice_spec];
167 template <
class... QueryDDims>
168 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
const
170 return view_type(*
this)[slice_spec];
174 template <
class... QueryDDims>
175 auto operator[](DiscreteElement<QueryDDims...>
const& slice_spec)
177 return span_view()[slice_spec];
181 template <
class... QueryDDims>
183 requires(is_discrete_domain_v<SupportType>)
185 return span_view()[odomain];
189 template <
class... QueryDDims>
191 requires(is_discrete_domain_v<SupportType>)
193 return span_view()[odomain];
197
198
199
200 template <
concepts::discrete_element... DElems>
201 element_type
const&
operator()(DElems
const&... delems)
const noexcept
204 SupportType::rank() == (0 + ... + DElems::size()),
205 "Invalid number of dimensions");
206 assert(
this->m_domain.contains(delems...));
207 return DDC_MDSPAN_ACCESS_OP(
208 this->m_allocation_mdspan,
209 detail::array(
this->m_domain.distance_from_front(delems...)));
213
214
215
216 template <
concepts::discrete_vector... DVects>
217 element_type
const&
operator()(DVects
const&... dvects)
const noexcept
218 requires(
sizeof...(DVects) != 0)
221 SupportType::rank() == (0 + ... + DVects::size()),
222 "Invalid number of dimensions");
223 return DDC_MDSPAN_ACCESS_OP(
224 this->m_allocation_mdspan,
225 detail::array(discrete_vector_type(dvects...)));
229
230
231
232 template <
concepts::discrete_element... DElems>
233 element_type&
operator()(DElems
const&... delems)
noexcept
236 SupportType::rank() == (0 + ... + DElems::size()),
237 "Invalid number of dimensions");
238 assert(
this->m_domain.contains(delems...));
239 return DDC_MDSPAN_ACCESS_OP(
240 this->m_allocation_mdspan,
241 detail::array(
this->m_domain.distance_from_front(delems...)));
245
246
247
248 template <
concepts::discrete_vector... DVects>
249 element_type&
operator()(DVects
const&... dvects)
noexcept
250 requires(
sizeof...(DVects) != 0)
253 SupportType::rank() == (0 + ... + DVects::size()),
254 "Invalid number of dimensions");
255 return DDC_MDSPAN_ACCESS_OP(
256 this->m_allocation_mdspan,
257 detail::array(discrete_vector_type(dvects...)));
261
262
263 char const*
label()
const
265 return m_label.c_str();
269
270
273 return base_type::data_handle();
277
278
281 return base_type::data_handle();
285
286
289 return base_type::allocation_mdspan();
293
294
297 return base_type::allocation_mdspan();
301
302
305 auto s =
this->allocation_mdspan();
306 auto kokkos_layout = detail::build_kokkos_layout(
309 std::make_index_sequence<SupportType::rank()> {});
311 detail::mdspan_to_kokkos_element_t<ElementType, SupportType::rank()>,
312 decltype(kokkos_layout),
313 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
317
318
321 auto s =
this->allocation_mdspan();
322 auto kokkos_layout = detail::build_kokkos_layout(
325 std::make_index_sequence<SupportType::rank()> {});
327 detail::mdspan_to_kokkos_element_t<ElementType
const, SupportType::rank()>,
328 decltype(kokkos_layout),
329 typename Allocator::memory_space>(s.data_handle(), kokkos_layout);
334 return view_type(*
this);
339 return view_type(*
this);
344 return span_type(*
this);
348template <
class SupportType,
class Allocator>
349Chunk(std::string
const&, SupportType
const&, Allocator)
352template <
class SupportType,
class Allocator>
353Chunk(SupportType
const&, Allocator)
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
char const * label() const
Returns the label of the Chunk.
const_allocation_mdspan_type allocation_mdspan() const
Provide a mdspan 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.
auto operator[](DiscreteDomain< QueryDDims... > const &odomain)
Slice out some dimensions.
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.
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
auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Slice out some dimensions.
element_type const & operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec)
Slice out some dimensions.
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
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 >