Low-Level Abstraction of Memory Access
AoSoA.hpp
Go to the documentation of this file.
1 // Copyright 2022 Bernhard Manfred Gruber
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #pragma once
5 
6 #include "Common.hpp"
7 
8 #include <limits>
9 
10 namespace llama::mapping
11 {
16  template<typename RecordDim, std::size_t VectorRegisterBits>
17  inline constexpr std::size_t maxLanes = []() constexpr
18  {
19  auto max = std::numeric_limits<std::size_t>::max();
20  forEachLeafCoord<RecordDim>(
21  [&](auto rc)
22  {
23  using AttributeType = GetType<RecordDim, decltype(rc)>;
24  max = std::min(max, VectorRegisterBits / (sizeof(AttributeType) * CHAR_BIT));
25  });
26  return max;
27  }();
28 
37  template<
38  typename TArrayExtents,
39  typename TRecordDim,
40  typename TArrayExtents::value_type Lanes,
41  FieldAlignment TFieldAlignment = FieldAlignment::Align,
42  typename TLinearizeArrayIndexFunctor = LinearizeArrayIndexRight,
43  template<typename> typename PermuteFields = PermuteFieldsInOrder>
44  struct AoSoA : MappingBase<TArrayExtents, TRecordDim>
45  {
46  private:
48  using size_type = typename Base::size_type;
49 
50  public:
51  inline static constexpr typename TArrayExtents::value_type lanes = Lanes;
52  inline static constexpr FieldAlignment fieldAlignment = TFieldAlignment;
53  using LinearizeArrayIndexFunctor = TLinearizeArrayIndexFunctor;
54  using Permuter = PermuteFields<FlatRecordDim<TRecordDim>>;
55  inline static constexpr std::size_t blobCount = 1;
56 
57 #if defined(__NVCC__) && __CUDACC_VER_MAJOR__ >= 12
58  using Base::Base;
59 #else
60  constexpr AoSoA() = default;
61 
62  LLAMA_FN_HOST_ACC_INLINE constexpr explicit AoSoA(TArrayExtents extents, TRecordDim = {}) : Base(extents)
63  {
64  }
65 #endif
66 
67  LLAMA_FN_HOST_ACC_INLINE constexpr auto blobSize(size_type) const -> size_type
68  {
69  const auto rs = static_cast<size_type>(
70  flatSizeOf<typename Permuter::FlatRecordDim, fieldAlignment == FieldAlignment::Align>);
71  return roundUpToMultiple(LinearizeArrayIndexFunctor{}.size(Base::extents()) * rs, Lanes * rs);
72  }
73 
74  template<std::size_t... RecordCoords>
76  typename Base::ArrayIndex ai,
78  {
80  }
81 
82  // Exposed for aosoaCommonBlockCopy. Should be private ...
83  template<std::size_t... RecordCoords>
85  size_type flatArrayIndex,
87  {
88  constexpr std::size_t flatFieldIndex =
89 #if defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ <= 6
90  *& // mess with nvcc compiler state to workaround bug
91 #endif
92  Permuter::template permute<flatRecordCoord<TRecordDim, RecordCoord<RecordCoords...>>>;
93  const auto blockIndex = flatArrayIndex / Lanes;
94  const auto laneIndex = flatArrayIndex % Lanes;
95  const auto offset
96  = static_cast<size_type>(
97  flatSizeOf<typename Permuter::FlatRecordDim, fieldAlignment == FieldAlignment::Align> * Lanes)
98  * blockIndex
99  + static_cast<size_type>(flatOffsetOf<
100  typename Permuter::FlatRecordDim,
101  flatFieldIndex,
103  * Lanes
104  + static_cast<size_type>(sizeof(GetType<TRecordDim, RecordCoord<RecordCoords...>>)) * laneIndex;
105  return {0, offset};
106  }
107  };
108 
112  template<
113  std::size_t Lanes,
115  typename LinearizeArrayIndexFunctor = LinearizeArrayIndexRight,
116  template<typename> typename PermuteFields = PermuteFieldsInOrder>
117  struct BindAoSoA
118  {
119  template<typename ArrayExtents, typename RecordDim>
121  };
122 
124  template<typename Mapping>
125  inline constexpr bool isAoSoA = false;
126 
128  template<
129  typename AD,
130  typename RD,
131  typename AD::value_type L,
132  FieldAlignment A,
133  typename Lin,
134  template<typename>
135  typename Perm>
136  inline constexpr bool isAoSoA<AoSoA<AD, RD, L, A, Lin, Perm>> = true;
137 } // namespace llama::mapping
#define LLAMA_EXPORT
Definition: macros.hpp:192
#define LLAMA_FN_HOST_ACC_INLINE
Definition: macros.hpp:96
constexpr bool isAoSoA
Definition: AoSoA.hpp:125
constexpr std::size_t maxLanes
Definition: AoSoA.hpp:17
typename internal::FlattenRecordDimImpl< RecordDim >::type FlatRecordDim
Returns a flat type list containing all leaf field types of the given record dimension.
Definition: Core.hpp:481
constexpr std::size_t flatRecordCoord
Definition: Core.hpp:517
constexpr std::size_t flatOffsetOf
The byte offset of an element in a type list ifs elements would be in a normal struct.
Definition: Core.hpp:634
constexpr auto roundUpToMultiple(Integral n, Integral mult) -> Integral
Returns the integral n rounded up to be a multiple of mult.
Definition: Core.hpp:578
typename internal::GetTypeImpl< RecordDim, RecordCoordOrTags... >::type GetType
Definition: Core.hpp:388
PermuteFields< FlatRecordDim< TRecordDim > > Permuter
Definition: AoSoA.hpp:54
constexpr auto blobSize(size_type) const -> size_type
Definition: AoSoA.hpp:67
static constexpr TArrayExtents::value_type lanes
Definition: AoSoA.hpp:51
constexpr auto blobNrAndOffset(size_type flatArrayIndex, RecordCoord< RecordCoords... >={}) const -> NrAndOffset< size_type >
Definition: AoSoA.hpp:84
constexpr AoSoA(TArrayExtents extents, TRecordDim={})
Definition: AoSoA.hpp:62
static constexpr FieldAlignment fieldAlignment
Definition: AoSoA.hpp:52
TLinearizeArrayIndexFunctor LinearizeArrayIndexFunctor
Definition: AoSoA.hpp:53
constexpr AoSoA()=default
static constexpr std::size_t blobCount
Definition: AoSoA.hpp:55
constexpr auto blobNrAndOffset(typename Base::ArrayIndex ai, RecordCoord< RecordCoords... > rc={}) const -> NrAndOffset< size_type >
Definition: AoSoA.hpp:75
typename ArrayExtents::value_type size_type
Definition: Common.hpp:25
typename ArrayExtents::Index ArrayIndex
Definition: Common.hpp:24
constexpr auto extents() const -> ArrayExtents
Definition: Common.hpp:35