Gyselalib++
vector_field.hpp
1 // SPDX-License-Identifier: MIT
2 
3 #pragma once
4 #include "ddc_alias_inline_functions.hpp"
5 #include "ddc_aliases.hpp"
6 #include "vector_field_mem.hpp"
7 
8 
9 template <
10  class ElementType,
11  class IdxRangeType,
12  class NDTag,
13  class MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
14  class LayoutStridedPolicy = std::experimental::layout_right>
15 class VectorField;
16 
17 template <
18  class ElementType,
19  class IdxRangeType,
20  class NDTag,
21  class MemorySpace,
22  class LayoutStridedPolicy>
23 inline constexpr bool enable_vector_field<
25 
26 template <
27  class ElementType,
28  class IdxRangeType,
29  class NDTag,
30  class MemorySpace,
31  class LayoutStridedPolicy>
32 inline constexpr bool enable_data_access_methods<
34 
35 template <
36  class ElementType,
37  class IdxRangeType,
38  class NDTag,
39  class MemorySpace,
40  class LayoutStridedPolicy>
41 inline constexpr bool enable_borrowed_vector_field<
43 
44 
54 template <
55  class ElementType,
56  class IdxRangeType,
57  class NDTag,
58  class MemorySpace,
59  class LayoutStridedPolicy>
61  : public VectorFieldCommon<
62  Field<ElementType, IdxRangeType, MemorySpace, LayoutStridedPolicy>,
63  NDTag>
64 {
65 public:
69  using field_type = Field<ElementType, IdxRangeType, MemorySpace, LayoutStridedPolicy>;
70 
71 private:
73 
74 public:
76  using element_type = typename base_type::element_type;
77 
78  using typename base_type::NDTypeTag;
79 
80  using typename base_type::chunk_span_type;
81  using typename base_type::chunk_view_type;
82 
83 public:
91  using span_type
97  using view_type
99 
106  using layout_type = LayoutStridedPolicy;
107 
113  using discrete_domain_type = typename base_type::discrete_domain_type;
116 
120  using memory_space = typename field_type::memory_space;
121 
122 private:
123  template <class, class, class, class, class>
124  friend class VectorField;
125 
126  template <class OElementType, class Allocator, std::size_t... Is>
127  KOKKOS_FUNCTION constexpr VectorField(
129  std::index_sequence<Is...> const&) noexcept
130  : base_type((field_type(ddcHelper::get<ddc::type_seq_element_t<Is, NDTag>>(other)))...)
131  {
132  }
133 
137  // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
138  template <
139  class OElementType,
140  std::size_t... Is,
141  class SFINAEElementType = ElementType,
142  class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
143  class Allocator>
144  KOKKOS_FUNCTION constexpr VectorField(
145  VectorFieldMem<OElementType, IdxRangeType, NDTag, Allocator> const& other,
146  std::index_sequence<Is...> const&) noexcept
147  : base_type((field_type(ddcHelper::get<ddc::type_seq_element_t<Is, NDTag>>(other)))...)
148  {
149  }
150 
154  template <class OElementType, std::size_t... Is>
155  KOKKOS_FUNCTION constexpr VectorField(
156  VectorField<
157  OElementType,
159  NDTag,
160  MemorySpace,
161  LayoutStridedPolicy> const& other,
162  std::index_sequence<Is...> const&) noexcept
163  : base_type((field_type(ddcHelper::get<ddc::type_seq_element_t<Is, NDTag>>(other)))...)
164  {
165  }
166 
167  template <class SliceType, std::size_t... Is>
168  constexpr auto get_slice(SliceType const& slice_spec, std::index_sequence<Is...> const&)
169  {
170  auto chunk_slices = std::make_tuple(
171  this->template get<ddc::type_seq_element_t<Is, NDTag>>()[slice_spec]...);
172  using FieldType = std::tuple_element_t<0, decltype(chunk_slices)>;
173  return VectorField<
174  ElementType,
175  typename FieldType::discrete_domain_type,
176  NDTag,
177  typename FieldType::memory_space,
178  typename FieldType::layout_type>(std::move(std::get<Is>(chunk_slices))...);
179  }
180 
185  template <class... ODDims, typename T, T... ints>
186  KOKKOS_FUNCTION element_type
187  operator()(Idx<ODDims...> const& delems, std::integer_sequence<T, ints...>) const noexcept
188  {
189  return element_type((base_type::m_values[ints](delems))...);
190  }
191 
192 public:
194  KOKKOS_DEFAULTED_FUNCTION constexpr VectorField() = default;
195 
197  KOKKOS_DEFAULTED_FUNCTION ~VectorField() = default;
198 
202  KOKKOS_DEFAULTED_FUNCTION constexpr VectorField(VectorField const& other) = default;
203 
207  KOKKOS_DEFAULTED_FUNCTION constexpr VectorField(VectorField&& other) = default;
208 
212  template <class OElementType, class Allocator>
213  KOKKOS_FUNCTION explicit constexpr VectorField(
215  : VectorField(other, std::make_index_sequence<base_type::NDims> {})
216  {
217  }
218 
222  // Disabled by SFINAE in the case of `ElementType` is not `const` to avoid write access
223  template <
224  class OElementType,
225  class SFINAEElementType = ElementType,
226  class = std::enable_if_t<std::is_const_v<SFINAEElementType>>,
227  class Allocator>
228  KOKKOS_FUNCTION explicit constexpr VectorField(
229  VectorFieldMem<OElementType, IdxRangeType, NDTag, Allocator> const& other) noexcept
230  : VectorField(other, std::make_index_sequence<base_type::NDims> {})
231  {
232  }
233 
234  template <class OElementType, class Allocator>
236 
240  template <class OElementType>
241  KOKKOS_FUNCTION constexpr VectorField(VectorField<
242  OElementType,
244  NDTag,
245  MemorySpace,
246  LayoutStridedPolicy> const& other) noexcept
247  : VectorField(other, std::make_index_sequence<base_type::NDims> {})
248  {
249  }
250 
255  template <
256  class... OElementType,
257  class = std::enable_if_t<
258  std::conjunction_v<std::is_same<OElementType, ElementType>...>>,
259  class = std::enable_if_t<sizeof...(OElementType) == base_type::NDims>>
260  KOKKOS_FUNCTION VectorField(index_range_type const& idx_range, OElementType*... ptr)
261  : base_type((field_type(ptr, idx_range))...)
262  {
263  }
264 
268  template <
269  class... FieldType,
270  class = std::enable_if_t<std::conjunction_v<std::is_same<FieldType, field_type>...>>>
271  KOKKOS_FUNCTION constexpr VectorField(FieldType... fields) : base_type(std::move(fields)...)
272  {
273  }
274 
279  KOKKOS_DEFAULTED_FUNCTION constexpr VectorField& operator=(VectorField const& other) = default;
280 
285  KOKKOS_DEFAULTED_FUNCTION constexpr VectorField& operator=(VectorField&& other) = default;
286 
295  constexpr view_type span_cview() const
296  {
297  return view_type(*this);
298  }
299 
308  constexpr span_type span_view() const
309  {
310  return *this;
311  }
312 
317  template <class... ODDims>
318  KOKKOS_FUNCTION element_type operator()(Idx<ODDims> const&... delems) const noexcept
319  {
320  Idx<ODDims...> delem_idx(delems...);
321  return this->
322  operator()(delem_idx, std::make_integer_sequence<int, element_type::size()> {});
323  }
324 
329  template <class... ODDims, class = std::enable_if_t<sizeof...(ODDims) != 1>>
330  KOKKOS_FUNCTION element_type operator()(Idx<ODDims...> const& delems) const noexcept
331  {
332  return this->operator()(delems, std::make_integer_sequence<int, element_type::size()> {});
333  }
334 
335 
346  template <class... QueryDDims>
347  constexpr auto operator[](Idx<QueryDDims...> const& slice_spec)
348  {
349  return get_slice(slice_spec, std::make_index_sequence<base_type::NDims> {});
350  }
351 
361  template <class... QueryDDims>
362  constexpr auto operator[](IdxRange<QueryDDims...> const& oidx_range)
363  {
364  return get_slice(oidx_range, std::make_index_sequence<base_type::NDims> {});
365  }
366 
372  template <class QueryTag>
373  inline constexpr chunk_span_type get() const noexcept
374  {
375  static_assert(
376  ddc::in_tags_v<QueryTag, NDTypeTag>,
377  "requested Tag absent from TaggedVector");
378  return base_type::m_values[ddc::type_seq_rank_v<QueryTag, NDTypeTag>].span_view();
379  }
380 };
381 
382 template <
383  class ElementType,
384  class IdxRangeType,
385  class NDTag,
386  class MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
387  class LayoutStridedPolicy = std::experimental::layout_right>
388 using VectorConstField
Definition: vector_field_common.hpp:9
Pre-declaration of VectorFieldMem.
Definition: vector_field_mem.hpp:54
A class which holds multiple (scalar) fields in order to represent a vector field.
Definition: vector_field.hpp:64
constexpr chunk_span_type get() const noexcept
Get the Field describing the component in the QueryTag direction.
Definition: vector_field.hpp:373
constexpr auto operator[](IdxRange< QueryDDims... > const &oidx_range)
Slice out some dimensions.
Definition: vector_field.hpp:362
typename base_type::discrete_domain_type discrete_domain_type
The type of the index range on which the field is defined.
Definition: vector_field.hpp:113
typename base_type::element_type element_type
The type of an element in one of the Fields comprising the VectorField.
Definition: vector_field.hpp:76
constexpr KOKKOS_DEFAULTED_FUNCTION VectorField(VectorField const &other)=default
Constructs a new VectorField by copy, yields a new view to the same data.
KOKKOS_FUNCTION VectorField(index_range_type const &idx_range, OElementType *... ptr)
Constructs a new VectorField from scratch.
Definition: vector_field.hpp:260
KOKKOS_FUNCTION element_type operator()(Idx< ODDims > const &... delems) const noexcept
Element access using a list of Idxs.
Definition: vector_field.hpp:318
typename field_type::memory_space memory_space
The type of the memory space where the field is saved (CPU vs GPU).
Definition: vector_field.hpp:120
constexpr KOKKOS_FUNCTION VectorField(FieldType... fields)
Constructs a new VectorField containing references to Field.
Definition: vector_field.hpp:271
constexpr KOKKOS_DEFAULTED_FUNCTION VectorField & operator=(VectorField const &other)=default
Copy-assigns a new value to this VectorField, yields a new view to the same data.
constexpr view_type span_cview() const
Get a constant reference to the vector field referred to by this vector field span.
Definition: vector_field.hpp:295
VectorField< const ElementType, IdxRangeType, NDTag, MemorySpace, LayoutStridedPolicy > view_type
A type which can hold a constant reference to a VectorFieldMem.
Definition: vector_field.hpp:98
constexpr KOKKOS_DEFAULTED_FUNCTION VectorField & operator=(VectorField &&other)=default
Move-assigns a new value to this VectorField.
Field< ElementType, IdxRangeType, MemorySpace, LayoutStridedPolicy > field_type
Type describing the object which can be extracted from this VectorField using the get<> function.
Definition: vector_field.hpp:69
KOKKOS_DEFAULTED_FUNCTION ~VectorField()=default
VectorField destructor.
constexpr KOKKOS_FUNCTION VectorField(VectorField< OElementType, index_range_type, NDTag, MemorySpace, LayoutStridedPolicy > const &other) noexcept
Constructs a new VectorField by copy of a chunk, yields a new view to the same data.
Definition: vector_field.hpp:241
constexpr span_type span_view() const
Get a modifiable reference to the vector field referred to by this vector field span.
Definition: vector_field.hpp:308
constexpr KOKKOS_FUNCTION VectorField(VectorFieldMem< OElementType, IdxRangeType, NDTag, Allocator > &other) noexcept
Constructs a new VectorField from a VectorFieldMem, yields a new view to the same data.
Definition: vector_field.hpp:213
LayoutStridedPolicy layout_type
Type describing the way in which the data is laid out in the Field memory.
Definition: vector_field.hpp:106
constexpr KOKKOS_DEFAULTED_FUNCTION VectorField(VectorField &&other)=default
Constructs a new VectorField by move.
constexpr auto operator[](Idx< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition: vector_field.hpp:347
constexpr KOKKOS_DEFAULTED_FUNCTION VectorField()=default
Empty VectorField.
discrete_domain_type index_range_type
The IdxRange on which the fields in this object are defined.
Definition: vector_field.hpp:115
A class which describes the real space in the temporal direction.
Definition: geometry.hpp:45