QGpCoreTools/MatrixMultiply.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2008-07-15
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef MATRIXMULTIPLY_H
00029 #define MATRIXMULTIPLY_H
00030 
00031 #include "AbstractNumericalKey.h"
00032 #include "AbstractNumericalCache.h"
00033 #include "QGpCoreToolsDLLExport.h"
00034 
00035 namespace QGpCoreTools {
00036 
00037 class QGPCORETOOLS_EXPORT MatrixMultiplyKey : public AbstractNumericalKey
00038 {
00039   DECLARE_NUMERICALKEY(MatrixMultiplyKey)
00040 public:
00041   MatrixMultiplyKey(int nRows1, int commonDim, int nColumns2) {
00042     _nRows1=nRows1;
00043     _commonDim=commonDim;
00044     _nColumns2=nColumns2;
00045   }
00046   virtual bool operator==(const AbstractNumericalKey& o) {
00047     const MatrixMultiplyKey& mo=static_cast<const MatrixMultiplyKey&>(o);
00048     return _nRows1==mo._nRows1 && _commonDim==mo._commonDim && _nColumns2==mo._nColumns2;
00049   }
00050   virtual int hash() const {return indexCount();}
00051   inline virtual AbstractNumericalCache * createCache();
00052   inline virtual int byteCount() const;
00053 
00054   int nRows1() const {return _nRows1;}
00055   int commonDim() const {return _commonDim;}
00056   int nColumns2() const {return _nColumns2;}
00057   int indexCount() const {return _nRows1*_nColumns2*_commonDim;}
00058 private:
00059   int _nRows1, _commonDim, _nColumns2;
00060 };
00061 
00062 class QGPCORETOOLS_EXPORT MatrixMultiply : public AbstractNumericalCache
00063 {
00064 public:
00065   MatrixMultiply(MatrixMultiplyKey * key);
00066   ~MatrixMultiply();
00067 
00068   static inline const MatrixMultiply * begin(int nRows1, int commonDim, int nColumns2);
00069   virtual void init();
00070 
00071   class IndexMap
00072   {
00073   public:
00074     IndexMap() {}
00075     void set(int rIndex, int f1Index, int f2Index) {
00076       _resultIndex=rIndex;
00077       _factor1Index=f1Index;
00078       _factor2Index=f2Index;
00079     }
00080 
00081     int resultIndex() const {return _resultIndex;}
00082     int factor1Index() const {return _factor1Index;}
00083     int factor2Index() const {return _factor2Index;}
00084   private:
00085     int _resultIndex, _factor1Index, _factor2Index;
00086   };
00087   inline int indexCount() const;
00088   const IndexMap& indexMap(int i) const {return _indexes[i];}
00089 
00090   static inline bool isValid(uint nRows1, uint commonDim, uint nColumns2);
00091 private:
00092   IndexMap * _indexes;
00093 };
00094 
00095 inline const MatrixMultiply * MatrixMultiply::begin(int nRows1, int commonDim, int nColumns2)
00096 {
00097   return static_cast<const MatrixMultiply *>(
00098       AbstractNumericalCache::begin(new MatrixMultiplyKey(nRows1, commonDim, nColumns2)));
00099 }
00100 
00101 inline AbstractNumericalCache * MatrixMultiplyKey::createCache( )
00102 {
00103   return new MatrixMultiply(this);
00104 }
00105 
00106 inline int MatrixMultiplyKey::byteCount() const
00107 {
00108   return indexCount()*sizeof(MatrixMultiply::IndexMap);
00109 }
00110 
00111 inline int MatrixMultiply::indexCount() const
00112 {
00113   const MatrixMultiplyKey& k=static_cast<const MatrixMultiplyKey&>(key());
00114   return k.indexCount();
00115 }
00116 
00117 inline bool MatrixMultiply::isValid(uint nRows1, uint commonDim, uint nColumns2)
00118 {
00119   return nRows1<256 && commonDim<256 && nColumns2<256;
00120 }
00121 
00122 } // namespace QGpCoreTools
00123 
00124 #endif // MATRIXMULTIPLY_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines