Oskar Weigl
/
Eurobot2013
We are going to win! wohoo
Embed:
(wiki syntax)
Show/hide line numbers
MMProduct.h
00001 /* 00002 * Tiny Vector Matrix Library 00003 * Dense Vector Matrix Libary of Tiny size using Expression Templates 00004 * 00005 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * $Id: MMProduct.h,v 1.24 2007-06-23 15:58:59 opetzold Exp $ 00022 */ 00023 00024 #ifndef TVMET_XPR_MMPRODUCT_H 00025 #define TVMET_XPR_MMPRODUCT_H 00026 00027 #include <tvmet/meta/Gemm.h> 00028 #include <tvmet/loop/Gemm.h> 00029 00030 namespace tvmet { 00031 00032 00033 /** 00034 * \class XprMMProduct MMProduct.h "tvmet/xpr/MMProduct.h" 00035 * \brief Expression for matrix-matrix product. 00036 * Using formula: 00037 * \f[ 00038 * M_1\,M_2 00039 * \f] 00040 * \note The Rows2 has to be equal to Cols1. 00041 */ 00042 template<class E1, std::size_t Rows1, std::size_t Cols1, 00043 class E2, std::size_t Cols2> 00044 class XprMMProduct 00045 : public TvmetBase< XprMMProduct<E1, Rows1, Cols1, E2, Cols2> > 00046 { 00047 private: 00048 XprMMProduct(); 00049 XprMMProduct& operator=(const XprMMProduct&); 00050 00051 public: 00052 typedef typename PromoteTraits< 00053 typename E1::value_type, 00054 typename E2::value_type 00055 >::value_type value_type; 00056 00057 public: 00058 /** Complexity counter. */ 00059 enum { 00060 ops_lhs = E1::ops, 00061 ops_rhs = E2::ops, 00062 M = Rows1 * Cols1 * Cols2, 00063 N = Rows1 * (Cols1 - 1) * Cols2, 00064 ops_plus = M * NumericTraits<value_type>::ops_plus, 00065 ops_muls = N * NumericTraits<value_type>::ops_muls, 00066 ops = ops_plus + ops_muls, 00067 use_meta = Rows1*Cols2 < TVMET_COMPLEXITY_MM_TRIGGER ? true : false 00068 }; 00069 00070 public: 00071 /** Constructor. */ 00072 explicit XprMMProduct(const E1& lhs, const E2& rhs) 00073 : m_lhs(lhs), m_rhs(rhs) 00074 { } 00075 00076 /** Copy Constructor. Not explicit! */ 00077 #if defined(TVMET_OPTIMIZE_XPR_MANUAL_CCTOR) 00078 XprMMProduct(const XprMMProduct& e) 00079 : m_lhs(e.m_lhs), m_rhs(e.m_rhs) 00080 { } 00081 #endif 00082 00083 private: 00084 /** Wrapper for meta gemm. */ 00085 static inline 00086 value_type do_gemm(dispatch<true>, const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) { 00087 return meta::gemm<Rows1, Cols1, 00088 Cols2, 00089 0>::prod(lhs, rhs, i, j); 00090 } 00091 00092 /** Wrapper for loop gemm. */ 00093 static inline 00094 value_type do_gemm(dispatch<false>, const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) { 00095 return loop::gemm<Rows1, Cols1, Cols2>::prod(lhs, rhs, i, j); 00096 } 00097 00098 public: 00099 /** index operator for arrays/matrices */ 00100 value_type operator()(std::size_t i, std::size_t j) const { 00101 TVMET_RT_CONDITION((i < Rows1) && (j < Cols2), "XprMMProduct Bounce Violation") 00102 return do_gemm(dispatch<use_meta>(), m_lhs, m_rhs, i, j); 00103 } 00104 00105 public: // debugging Xpr parse tree 00106 void print_xpr(std::ostream& os, std::size_t l=0) const { 00107 os << IndentLevel(l++) 00108 << "XprMMProduct[" 00109 << (use_meta ? "M" : "L") << ", O=" << ops 00110 << ", (O1=" << ops_lhs << ", O2=" << ops_rhs << ")]<" 00111 << std::endl; 00112 m_lhs.print_xpr(os, l); 00113 os << IndentLevel(l) 00114 << "R1=" << Rows1 << ", C1=" << Cols1 << ",\n"; 00115 m_rhs.print_xpr(os, l); 00116 os << IndentLevel(l) 00117 << "C2=" << Cols2 << ",\n"; 00118 os << IndentLevel(--l) 00119 << ">," << std::endl; 00120 } 00121 00122 private: 00123 const E1 m_lhs; 00124 const E2 m_rhs; 00125 }; 00126 00127 00128 } // namespace tvmet 00129 00130 #endif // TVMET_XPR_MMPRODUCT_H 00131 00132 // Local Variables: 00133 // mode:C++ 00134 // tab-width:8 00135 // End:
Generated on Tue Jul 12 2022 19:50:03 by 1.7.2