TarzaNN
TarzaNN neural network simulator
C:/Users/albertlr/projects/TarzaNN/TarzaNN/MatrixOfMatrix.h
Go to the documentation of this file.
00001 /****************************************************************************
00002  **
00003  ** Copyright C 2002-2012 Laboratory for Active and Attentive Vision (LAAV), Department of Computer Science and Engineering, York University, Toronto, ON, Canada.
00004  ** All rights reserved.
00005  **
00006  ** This file is part of the TarzaNN Neural Network Simulator.
00007  **
00008  ** This file may be distributed and/or modified under the terms of the
00009  ** GNU General Public License version 2 as published by the Free Software
00010  ** Foundation and appearing in the file LICENSE.GPL included in the
00011  ** packaging of this file.
00012  **
00013  ** See http://www.tarzann.org/gpl/ for GPL licensing information.
00014  **
00015  ** Contact info@tarzann.org if any conditions of this licensing are
00016  ** not clear to you.
00017  **
00018  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00019  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  **
00021  ****************************************************************************/
00022 
00023 #ifndef MatrixOfMatrix_H_INCLUDED
00024 #define MatrixOfMatrix_H_INCLUDED
00025 #include <stdio.h>
00026 #include <assert.h>
00027 #include "Array2D.h"
00028 #include <QApplication>
00029 #include <QDataStream>
00030 #include <QDebug>
00031 #include <QProgressDialog>
00032 #include <exception>
00033 using namespace std;
00034 
00036 template <typename T>
00037         class MatrixOfMatrix:public Array2D<T*>{
00038         protected:
00039     int filterWidth, filterHeight;
00040     int inputWidth, inputHeight;
00041     static void getMemorySize(int& row, int& total, int width, int height){
00042     }
00043 
00044 public:
00045     MatrixOfMatrix( const uint32_t nXSize, const uint32_t nYSize, const uint32_t filterSizeX, const uint32_t filterSizeY, const uint32_t inputSizeX=0, const uint32_t inputSizeY=0)
00046         : Array2D<T*>(nXSize, nYSize), filterWidth(filterSizeX), filterHeight(filterSizeY),inputWidth(inputSizeX), inputHeight(inputSizeY){
00047 
00048         Array2D<T*>::memorySize= nXSize* nYSize*sizeof(T*);
00049         Array2D<T*>::data = calloc( 1, Array2D<T*>::memorySize+4);
00050         if (!Array2D<T*>::data){
00051             qDebug() << "Gating malloc error";
00052             exit(-ERROR_MATRIX_GATING_ERROR);
00053         }
00054         //              try{
00055         Array2D<T*>::rows= new T**[nYSize];
00056         //              }
00057         //              catch (std::bad_alloc&){
00058         //                      printf("Error allocating memory.\n");fflush(stdout);
00059         //                      exit(-1);
00060         //              }
00061 
00062         for (int i= 0; i<Array2D<T*>::height; i++)
00063             Array2D<T*>::rows[ i] = &(((T**)(Array2D<T*>::data))[i*Array2D<T*>::width]);
00064 
00065         for(int i=0; i<Array2D<T*>::height; i++)
00066             for(int j=0; j<Array2D<T*>::width; j++)
00067                 (Array2D<T*>::rows[i])[j]= new T(filterSizeX, filterSizeY);
00068     }
00069 
00071     ~MatrixOfMatrix(){
00072         for(uint32_t i=0; i<Array2D<T*>::height; i++)
00073             for(uint32_t j=0; j<Array2D<T*>::width; j++)
00074                 delete (Array2D<T*>::rows[i])[j];
00075     }
00076 
00084 #ifdef __GNUC__
00085     __attribute__((always_inline))
00086 #endif
00087             inline void setGU(uint32_t iuX, uint32_t iuY, uint32_t guX, uint32_t guY, float value){
00088         Array2D<float>* tempX=NULL;
00089         try {
00090             tempX=(*this)(iuX,iuY);
00091             (*tempX)(guX,guY)=value;
00092         }
00093         catch (...) {
00094             printf("%d %d %d %d %d %d\n", tempX->getXSize() , tempX->getYSize(), iuX, iuY, guX, guY);
00095             printf("EXCEPTION2\n");
00096             throw;
00097         }
00098     }
00099 
00100 #ifdef __GNUC__
00101     __attribute__((always_inline))
00102 #endif
00103             inline float getGU(uint32_t iuX, uint32_t iuY, uint32_t guX, uint32_t guY){
00104         Array2D<float>* tempX=(*this)(iuX,iuY);
00105         return (*tempX)(guX,guY);
00106     }
00107     //overloaded for more generic name
00108 #ifdef __GNUC__
00109     __attribute__((always_inline))
00110 #endif
00111             inline void set(uint32_t iuX, uint32_t iuY, uint32_t guX, uint32_t guY, float value){
00112         setGU( iuX, iuY, guX, guY, value);
00113     }
00114     //overloaded for more generic name
00115 #ifdef __GNUC__
00116     __attribute__((always_inline))
00117 #endif
00118             inline float get(uint32_t iuX, uint32_t iuY, uint32_t guX, uint32_t guY){
00119         return getGU( iuX, iuY, guX, guY);
00120 
00121     }
00122 
00123     uint32_t getInputWidth(){return inputWidth;}
00124     uint32_t getInputHeight(){return inputHeight;}
00125     uint32_t getFilterWidth(){return filterWidth;}
00126     uint32_t getFilterHeight(){return filterHeight;}
00127     void dump(){
00128         for(uint32_t i=0; i<Array2D<T*>::height; i++){
00129             for(uint32_t j=0; j<Array2D<T*>::width; j++){
00130                 printf("FB or GU (%d, %d)\n", i,j);
00131                 (Array2D<T*>::rows[i])[j]->printContent();
00132             }
00133         }
00134     }
00135 
00136     void save(QDataStream* outStream, QProgressDialog* progress, int* index){
00137 
00138         for(uint32_t i=0; i<Array2D<T*>::height; i++){
00139             for(uint32_t j=0; j<Array2D<T*>::width; j++){
00140                 if(progress)
00141                     progress->setValue(++(*index));
00142                 qApp->processEvents();
00143                 (Array2D<T*>::rows[i])[j]->save(outStream);
00144             }
00145         }
00146     }
00147 
00148     void read(QDataStream* inStream, QProgressDialog* progress, int* index){
00149 
00150         for(uint32_t i=0; i<Array2D<T*>::height; i++){
00151             for(uint32_t j=0; j<Array2D<T*>::width; j++){
00152                 progress->setValue(++(*index));
00153                 qApp->processEvents();
00154                 (Array2D<T*>::rows[i])[j]->read(inStream);
00155             }
00156         }
00157     }
00158 };
00159 #endif /*MatrixOfMatrix_H_INCLUDED*/
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines