|
| 1 | +#ifndef FSLINALG_MATRIX_PRODUCT_TRAITS_HPP |
| 2 | +#define FSLINALG_MATRIX_PRODUCT_TRAITS_HPP |
| 3 | + |
| 4 | +#include <cstddef> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +#include <FSLinalg/Matrix/MatrixProductChain.hpp> |
| 8 | + |
| 9 | +namespace FSLinalg |
| 10 | +{ |
| 11 | + |
| 12 | +template<class Lhs, class Rhs> class MatrixProduct; |
| 13 | + |
| 14 | +namespace detail |
| 15 | +{ |
| 16 | + |
| 17 | +template<class Expr> |
| 18 | +struct MatrixProductAnalyzerImpl |
| 19 | +{ |
| 20 | + static constexpr size_t length = 1; |
| 21 | + |
| 22 | + template<size_t n> requires(n == 0) |
| 23 | + using NthMatrix = Expr; |
| 24 | + |
| 25 | + template<size_t n> |
| 26 | + static constexpr const NthMatrix<n>& getMatrix(const Expr& expr, std::integral_constant<size_t, n>) { return expr; } |
| 27 | + |
| 28 | + template<size_t idx, size_t chainLenP1> requires (idx+1 <chainLenP1) |
| 29 | + static constexpr void fillDims(std::integral_constant<size_t, idx>, std::array<size_t, chainLenP1>& dims) { dims[idx] = Expr::nRows; dims[idx+1] = Expr::nCols; } |
| 30 | +}; |
| 31 | + |
| 32 | +template<class Lhs, class Rhs> |
| 33 | +struct MatrixProductAnalyzerImpl< MatrixProduct<Lhs, Rhs> > |
| 34 | +{ |
| 35 | + static constexpr size_t lhsLength = MatrixProductAnalyzerImpl<Lhs>::length; |
| 36 | + static constexpr size_t rhsLength = MatrixProductAnalyzerImpl<Rhs>::length; |
| 37 | + static constexpr size_t length = lhsLength + rhsLength; |
| 38 | + |
| 39 | + template<size_t n, class Enable = void> |
| 40 | + struct NthMatrixHelper; |
| 41 | + |
| 42 | + template<size_t n> |
| 43 | + struct NthMatrixHelper<n, std::enable_if_t<n < lhsLength>> |
| 44 | + { |
| 45 | + using Type = typename MatrixProductAnalyzerImpl<Lhs>::template NthMatrix<n>; |
| 46 | + }; |
| 47 | + |
| 48 | + template<size_t n> |
| 49 | + struct NthMatrixHelper<n, std::enable_if_t<lhsLength <= n and n < length>> |
| 50 | + { |
| 51 | + using Type = typename MatrixProductAnalyzerImpl<Rhs>::template NthMatrix<n - lhsLength>; |
| 52 | + }; |
| 53 | + |
| 54 | + template<size_t n> |
| 55 | + using NthMatrix = typename NthMatrixHelper<n>::Type; |
| 56 | + |
| 57 | + template<size_t n> |
| 58 | + static constexpr const NthMatrix<n>& getMatrix(const MatrixProduct<Lhs, Rhs>& expr, std::integral_constant<size_t, n>); |
| 59 | + |
| 60 | + template<size_t idx, size_t chainLenP1> requires (idx+1 <chainLenP1) |
| 61 | + static constexpr void fillDims(std::integral_constant<size_t, idx>, std::array<size_t, chainLenP1>& dims); |
| 62 | +}; |
| 63 | + |
| 64 | +} // namespace detail |
| 65 | + |
| 66 | +template<class Expr> |
| 67 | +struct MatrixProductAnalyzer |
| 68 | +{ |
| 69 | + static_assert(IsMatrix<Expr>::value, "Expr must be a matrix"); |
| 70 | + |
| 71 | + using Impl = detail::MatrixProductAnalyzerImpl<Expr>; |
| 72 | + using DimArray = std::array<size_t, Impl::length+1>; |
| 73 | + |
| 74 | + template<size_t n> using NthMatrix = typename Impl::template NthMatrix<n>; |
| 75 | + |
| 76 | + template<size_t n> |
| 77 | + static const NthMatrix<n>& getMatrix(const Expr& expr, std::integral_constant<size_t, n> ic) { return Impl::getMatrix(expr, ic); } |
| 78 | + |
| 79 | + static constexpr size_t getLength() { return Impl::length; } |
| 80 | + |
| 81 | + static constexpr DimArray getDims(); |
| 82 | + |
| 83 | + // we use an external template class as to not recompute everything for every product |
| 84 | + // if the optimal splitting for an chain with the same dims has already been computed |
| 85 | + // we can re-use it. |
| 86 | + static constexpr size_t getMinalCost() { return MatrixProductChain<getDims()>::template minCostAndSplit<0, getLength()>.first; } |
| 87 | + static constexpr size_t getOptimalSplit() { return MatrixProductChain<getDims()>::template minCostAndSplit<0, getLength()>.second; } |
| 88 | +private: |
| 89 | + template<size_t start, size_t end> requires(start <= end and end <= getLength()) |
| 90 | + struct OptimalBracketingHelper |
| 91 | + { |
| 92 | + static constexpr size_t split = MatrixProductChain<getDims()>::template minCostAndSplit<start, end>.second; |
| 93 | + |
| 94 | + static_assert(start <= split and split+1 < end+1, "invalid split"); |
| 95 | + |
| 96 | + using LhsBracketing = OptimalBracketingHelper<start, split>; |
| 97 | + using RhsBracketing = OptimalBracketingHelper<split, end>; |
| 98 | + |
| 99 | + using Lhs = typename LhsBracketing::Type; |
| 100 | + using Rhs = typename RhsBracketing::Type; |
| 101 | + |
| 102 | + using Type = MatrixProduct<Lhs, Rhs>; |
| 103 | + using ReBracketType = Type; |
| 104 | + |
| 105 | + static ReBracketType reBracket(const Expr& expr) { return ReBracketType(LhsBracketing::reBracket(expr), RhsBracketing::reBracket(expr)); } |
| 106 | + }; |
| 107 | + |
| 108 | + template<size_t idx> requires(idx < getLength()) |
| 109 | + struct OptimalBracketingHelper<idx,idx+1> |
| 110 | + { |
| 111 | + using Type = NthMatrix<idx>; |
| 112 | + using ReBracketType = const Type&; |
| 113 | + |
| 114 | + static ReBracketType reBracket(const Expr& expr) { return MatrixProductAnalyzer<Expr>::getMatrix(expr, std::integral_constant<size_t, idx>{}); } |
| 115 | + }; |
| 116 | +public: |
| 117 | + using OptimalBracketing = typename OptimalBracketingHelper<0, getLength()>::Type; |
| 118 | + using ReBracketType = typename OptimalBracketingHelper<0, getLength()>::ReBracketType; |
| 119 | + |
| 120 | + static ReBracketType reBracket(const Expr& expr) { return OptimalBracketingHelper<0, getLength()>::reBracket(expr); } |
| 121 | +}; |
| 122 | + |
| 123 | +} // namespace FSLinalg |
| 124 | + |
| 125 | +#endif // FSLINALG_MATRIX_PRODUCT_TRAITS_HPP |
0 commit comments