Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignedIndexing.hh
Go to the documentation of this file.
1 ///
2 /// @file AlignedIndexing.hh
3 /// @brief Sparse tensor indexer
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_MATH_MULTIDIM_ALIGNEDINDEXING
13 #define HAMMER_MATH_MULTIDIM_ALIGNEDINDEXING
14 
15 #include <map>
16 #include <utility>
17 
19 
20 namespace Hammer {
21 
22 
23  enum class IndexPairMember { Left, Right, Both };
24 
25  namespace MultiDimensional {
26 
28  public:
30 
31  AlignedIndexing(IndexList dimensions);
32 
33  AlignedIndexing(const AlignedIndexing&) = default;
34  AlignedIndexing(AlignedIndexing&&) = default;
35  AlignedIndexing& operator=(const AlignedIndexing&) = default;
37 
38  ~AlignedIndexing() = default;
39 
40  public:
41  /// @brief rank of the tensor
42  /// @return the number of components
43  size_t rank() const;
44 
45  /// @brief dimension of a specific component
46  /// @param[in] index the component index
47  /// @return the dimension
48  IndexType dim(IndexType index) const;
49 
50  /// @brief get the dimensions of all the indices at once
51  /// @return the list of dimensions
52  const IndexList& dims() const;
53 
54  /// @brief the number of elements (product of all the dimensions)
55  PositionType numValues() const;
56 
57  /// @brief convert the indices into the (aligned) position indicizing
58  /// a sparse tensor container organized as row-major
59  /// @param[in] indices the element indices
60  /// @return the absolute (aligned) position
61  PositionType indicesToPos(const IndexList& indices) const;
62 
63  PositionType indicesToPos(IndexList::const_iterator first, IndexList::const_iterator last) const;
64 
65  /// @brief convert the absolute aligned position to the absolute unaligned position
66  /// that can be used with a `SequentialIndexing` object
67  /// @param[in] alignedPosition the absolute aligned position
68  /// @return the absolute unaligned position
69  PositionType alignedPosToPos(PositionType alignedPosition) const;
70 
71  /// @brief convert the absolute unaligned position (e.g. coming from a `SequentialIndexing` object)
72  /// to the absolute aligned position
73  /// @param[in] position the absolute unaligned position
74  /// @return the absolute aligned position
76 
77  /// @brief check that the indices are within range for each component
78  /// @param[in] indices the element indices
79  /// @return true if all indices are OK
80  bool checkValidIndices(const IndexList& indices) const;
81 
82  bool checkValidIndices(IndexList::const_iterator first, IndexList::const_iterator last) const;
83 
84  /// @brief convert the absolute aligned position (in row-major convention)
85  /// into the list of indices
86  /// @param[in] alignedPosition the absolute aligned position
87  /// @param[out] result the indices
88  void posToIndices(PositionType alignedPosition, IndexList& result) const;
89 
90  /// @brief extract the value of the i-th index from an absolute aligned position
91  /// @param[in] alignedPosition the absolute position
92  /// @param[in] indexPosition the index coordinate i
93  /// @return the index value
94  IndexType ithIndexInPos(PositionType alignedPosition, IndexType indexPosition) const;
95 
96  /// @brief split an absolute aligned position by separating contracted and uncontracted indices
97  /// and returning the absolute position made of uncontracted indices and the coordinates
98  /// of the contracted ones properly reordered. This function is the workhorse of Dot and Trace.
99  /// The method accepts an optional coordinate list for the contracted indices: if that does not match
100  /// the coordinate list extracted from the position the function return 0 for the external position and an empty list
101  /// for the contracted indices.
102  /// @param[in] alignedPosition the absolute aligned index
103  /// @param[in] outerShiftsInnerPositions a list (of rank length) with a positional shift for each uncontracted index and the corresponding position for the contracted index
104  /// @param[in] isOuter a list (of rank length) marking whether an index is not contracted
105  /// @param[in] innerList
106  /// @param[in] innerAdded if present, the list of values that the contracted coordinates should have
107  /// @param[in] shouldCompare if present, the list of values that the contracted coordinates should have
108  /// @return a pair (absolute aligned position made of the uncontracted indices, reduced index list for the contracted indices reordered according to the shifts)
109  PositionType splitPosition(PositionType alignedPosition, const IndexList& outerShiftsInnerPositions,
110  const std::vector<bool>& isOuter, IndexList& innerList, std::vector<bool>& innerAdded,
111  bool shouldCompare = false) const;
112 
113  /// @brief build the input lists necessary for calling splitPosition based on which indices are being contracted
114  /// and whether the tensor indexed by this class is the on left or right of Dot or it is the argument of Trace
115  /// @param[in] pairs the list of contractions (pairs od index position being contracted
116  /// @param[in] which whether the tensor is the first or second argument of Dot or it is the argument of Trace
117  /// @return the lists to be fed to splitPosition (outerShiftsInnerPositions and isOuter) and the maximum aligned index of the uncontracted indices
118  std::tuple<IndexList, std::vector<bool>, PositionType> processShifts(const IndexPairList& pairs, IndexPairMember which) const;
119 
120  /// @brief get the maximum allowed value for the absolute position
121  /// @param[in] aligned whether the maximum absolute position is aligned or not
122  /// @return the maximum allowed absolute position
123  PositionType maxIndex(bool aligned = true) const;
124 
125  /// @brief extends an unaligned absolute position from a rank N-1 tensor to the corresponding
126  /// aligned position for this rank N tensor given the missing index and its value.
127  /// Used in AddAt
128  /// @param position the sub-tensor unaligned position
129  /// @param stride the stride (in the sub-tensor) of the index preceding the missing index
130  /// @param indexPosition the missing index coordinate
131  /// @param indexValue the missing index value
132  /// @return the corresponding absolute aligned position
133  PositionType extendPosition(PositionType position, PositionType stride, IndexType indexPosition,
134  IndexType indexValue) const;
135 
136  /// @brief extends an aligned absolute position from a rank N-1 tensor to the corresponding
137  /// aligned position for this rank N tensor given the missing index and its value.
138  /// Used in AddAt
139  /// @param alignedPosition the sub-tensor aligned position
140  /// @param indexPosition the missing index coordinate
141  /// @param indexValue the missing index value
142  /// @return the corresponding absolute aligned position
143  PositionType extendAlignedPosition(PositionType alignedPosition, IndexType indexPosition, IndexType indexValue) const;
144 
145  // /// @brief
146  // /// @param[in] index
147  // /// @param[in] positions
148  // /// @param[in] values
149  // /// @param[in] result
150  // /// @return
151  // bool isIndexMatchingSecond(size_t index, const IndexPairList& positions, const IndexList& values,
152  // IndexList& result) const;
153 
154  public:
155  template <typename BasicIndexing>
156  bool isSameShape(const BasicIndexing& other) const;
157 
158  bool isSameShape(const IndexList& indices) const;
159 
160  private:
161  void calc();
162  PositionType calcPadding(const IndexList& dimensions, IndexList& pads) const;
163  void calcMasks(const IndexList& dimensions, IndexList& masks) const;
164  void calcUnaligned(const IndexList& dimensions, const IndexList& pads,
165  PosIndexPairList& unaligned) const;
166 
167  IndexList _dimensions; ///< the dimensions of each tensor index
168  IndexList _alignPads; ///< the strides for each tensor index (necessary to convert coordinates to position in `_data`)
169  IndexList _alignMasks; ///< the strides for each tensor index (necessary to convert coordinates to position in `_data`)
173  };
174 
175  template <typename BasicIndexing>
176  bool AlignedIndexing::isSameShape(const BasicIndexing& other) const {
177  return isSameShape(other.dims());
178  }
179 
180  }
181 
182 } // namespace Hammer
183 
184 #endif
void calcMasks(const IndexList &dimensions, IndexList &masks) const
std::vector< PosIndexPair > PosIndexPairList
AlignedIndexing & operator=(const AlignedIndexing &)=default
std::vector< IndexPair > IndexPairList
size_t PositionType
void calcUnaligned(const IndexList &dimensions, const IndexList &pads, PosIndexPairList &unaligned) const
PositionType extendPosition(PositionType position, PositionType stride, IndexType indexPosition, IndexType indexValue) const
extends an unaligned absolute position from a rank N-1 tensor to the corresponding aligned position f...
IndexList _alignPads
the strides for each tensor index (necessary to convert coordinates to position in _data) ...
IndexType ithIndexInPos(PositionType alignedPosition, IndexType indexPosition) const
extract the value of the i-th index from an absolute aligned position
uint16_t IndexType
PositionType numValues() const
the number of elements (product of all the dimensions)
Forward declarations of types used in the tensor classes.
const IndexList & dims() const
get the dimensions of all the indices at once
size_t rank() const
rank of the tensor
PositionType alignedPosToPos(PositionType alignedPosition) const
convert the absolute aligned position to the absolute unaligned position that can be used with a Sequ...
std::vector< IndexType > IndexList
PositionType calcPadding(const IndexList &dimensions, IndexList &pads) const
PositionType posToAlignedPos(PositionType position) const
convert the absolute unaligned position (e.g.
IndexType dim(IndexType index) const
dimension of a specific component
PositionType maxIndex(bool aligned=true) const
get the maximum allowed value for the absolute position
bool checkValidIndices(const IndexList &indices) const
check that the indices are within range for each component
void posToIndices(PositionType alignedPosition, IndexList &result) const
convert the absolute aligned position (in row-major convention) into the list of indices ...
IndexList _dimensions
the dimensions of each tensor index
PositionType splitPosition(PositionType alignedPosition, const IndexList &outerShiftsInnerPositions, const std::vector< bool > &isOuter, IndexList &innerList, std::vector< bool > &innerAdded, bool shouldCompare=false) const
split an absolute aligned position by separating contracted and uncontracted indices and returning th...
PositionType indicesToPos(const IndexList &indices) const
convert the indices into the (aligned) position indicizing a sparse tensor container organized as row...
PositionType extendAlignedPosition(PositionType alignedPosition, IndexType indexPosition, IndexType indexValue) const
extends an aligned absolute position from a rank N-1 tensor to the corresponding aligned position for...
IndexList _alignMasks
the strides for each tensor index (necessary to convert coordinates to position in _data) ...
bool isSameShape(const BasicIndexing &other) const
std::tuple< IndexList, std::vector< bool >, PositionType > processShifts(const IndexPairList &pairs, IndexPairMember which) const
build the input lists necessary for calling splitPosition based on which indices are being contracted...