Main Page | Class List | File List | Class Members

CS_ARRAY.hpp

00001 
00002 #include<cstdlib>
00003 #include<string>
00004 #include<iostream>
00005 #include<sstream>
00006 
00007 #include "CACHE.hpp"
00008 #include "CS_MATRIX.hpp"
00009 
00010 #ifndef _H_CS_ARRAY
00011 #define _H_CS_ARRAY
00012 
00014 template < typename T > class CS_ARRAY {
00015    private:
00017         CS_ARRAY() {}
00018 
00019    protected:
00021         T* _array;
00022 
00024         unsigned long int _size;
00025 
00027         unsigned long int FAC;
00028 
00029    public:
00030 
00031         //static stuff first
00032 
00037         static std::string fDim( const CS_ARRAY& obj ) {
00038                 std::ostringstream oss;
00039                 oss << "( " << obj.getSize() << " x 1 )";
00040                 return oss.str();
00041         }
00042 
00043         //end static stuff
00044 
00045 
00047         std::string _descr;
00048 
00050         CS_ARRAY( unsigned long int size, std::string descr ) {
00051                 if ( descr == "" ) {
00052                         std::cerr << "Parameter description is mandatory during CS_ARRAY initialisation!" << std::endl;
00053                         exit( 1 );
00054                 }
00055 
00056                 if ( size == 0 ) {
00057                         std::cerr << "Size cannot be 0 during allocation of " << descr << std::endl;
00058                         exit( 1 );
00059                 }
00060 
00061                 _array = new T[size];
00062                 _size = size;
00063                 _descr = descr;
00064                 FAC = 0;
00065         }
00066 
00067         ~CS_ARRAY() {
00068                 delete [] _array;
00069         }
00070 
00072         unsigned long int size() { return _size; }
00073 
00075         inline
00076         T& operator[] ( unsigned long int i ) { return access( i ); }
00077 
00079         const T& const_access( unsigned long int i ) {
00080                 if( i < _size ) {
00081                         CACHE::getInstance()->access( &( _array[ i ] ), sizeof( T ), FAC );
00082                         return _array[ i ];
00083                 } else {
00084                         std::cerr << _descr << ": Index out of bounds! (" << i << "/" << this->_size << ")" << std::endl;
00085                         exit( !EXIT_SUCCESS );
00086                 }
00087         }
00088 
00090         T& unrecorded_access( const unsigned long int i ) { return _array[ i ]; }
00091 
00093         const T& unrecorded_const_access( const unsigned long int i ) const { return _array[ i ]; }
00094 
00096         T& access( unsigned long int i ) {
00097                 if( i < _size ) {
00098                         CACHE::getInstance()->access( &( _array[ i ] ), sizeof( T ), FAC );
00099                         return _array[ i ];
00100                 } else {
00101                         std::cerr << _descr << ": Index out of bounds! (" << i << "/" << this->_size << ")" << std::endl;
00102                         exit( !EXIT_SUCCESS );
00103                 }
00104         }
00105 
00107         unsigned long int getSize() const { return _size; }
00108 
00110         void unrecordedCopyTo( T* target ) {
00111                 for( unsigned long int i=0; i<_size; i++ )
00112                         target[ i ] = _array[ i ];
00113         }
00114 
00116         void unrecordedCopyFrom( T* target ) {
00117                 for( unsigned long int i=0; i<_size; i++ )
00118                         _array[ i ] = target[ i ];
00119         }
00120 
00126         void yaxpy( const CS_MATRIX< T >& A, const CS_ARRAY< T >& x ) {
00127                 if( A.cols() != x.getSize() ) {
00128                         std::cerr << A._descr << " (A) does not agree with " << x._descr << " (x) for y=Ax+y!" << std::endl;
00129                         std::cerr << CS_MATRIX< T >::fDim( A ) << " times " << CS_ARRAY< T >::fDim( x ) << std::endl;
00130                         exit( !EXIT_SUCCESS );
00131                 }
00132 
00133                 if( A.rows() != getSize() ) {
00134                         std::cerr << A._descr << " (A) does not agree with " << _descr << " (y) for y=Ax+y!" << std::endl;
00135                         std::cerr << CS_MATRIX< T >::fDim( A ) << " times " << CS_ARRAY< T >::fDim( *this ) << std::endl;
00136                         exit( !EXIT_SUCCESS );
00137                 }
00138 
00139                 for( unsigned int i=0; i<A.rows(); i++ )
00140                         for( unsigned int j=0; j<A.cols(); j++ ) {
00141                                 //preserve order of data access
00142                                 const T _a = A( i, j );
00143                                 const T _x = x[ j ];
00144                                 const T _y = access( i );
00145                                 access( i ) = _a * _x + _y;
00146                         }
00147         }
00148 
00155         void zaxpy( const CS_MATRIX< T >& A, const CS_ARRAY< T >& x, const CS_ARRAY< T >& y ) {
00156                 if( A.cols() != x.getSize() ) {
00157                         std::cerr << A._descr << " (A) does not agree with " << x._descr << " (x) for z=Ax+y!" << std::endl;
00158                         std::cerr << CS_MATRIX< T >::fDim( A ) << " X " << CS_ARRAY< T >::fDim( x ) << std::endl;
00159                         exit( !EXIT_SUCCESS );
00160                 }
00161 
00162                 if( A.rows() != y.getSize() ) {
00163                         std::cerr << A._descr << " (A) does not agree with " << y._descr << " (y) for z=Ax+y!" << std::endl;
00164                         std::cerr << CS_MATRIX< T >::fDim( A ) << " X " << CS_ARRAY< T >::fDim( y ) << std::endl;
00165                         exit( !EXIT_SUCCESS );
00166                 }
00167 
00168                 if( A.rows() != getSize() ) {
00169                         std::cerr << A._descr << " (A) does not agree with " << _descr << " (z) for z=Ax+y!" << std::endl;
00170                         std::cerr << CS_MATRIX< T >::fDim( A ) << " X " << CS_ARRAY< T >::fDim( this ) << std::endl;
00171                         exit( !EXIT_SUCCESS );
00172                 }
00173 
00174                 if( y.getSize() != getSize() ) {
00175                         std::cerr << y._descr << " (y) does not agree with " << _descr << " (z) for z=Ax+y!" << std::endl;
00176                         std::cerr << CS_ARRAY< T >::fDim( y ) << " X " << CS_ARRAY< T >::fDim( *this ) << std::endl;
00177                         exit( !EXIT_SUCCESS );
00178                 }
00179  
00180                 for( unsigned int i=0; i<A.rows(); i++ ) {
00181                         //preserve order of access
00182                         const T ao = A( i, 0 );
00183                         const T xo = x[ 0 ];
00184                         const T yo = y [ i ];
00185                         access( i ) = ao*xo+yo; // z[i] = A[i][0]*x[0]+y[i]
00186                         for( unsigned int j=1; j<A.cols(); j++ ) {
00187                                 const T _a = A( i, j );
00188                                 const T _x = x[ j ];
00189                                 const T _z = const_access( i );
00190                                 access( i ) = _a*_x + _z; // z[i] += A[i][j]*x[j]
00191                         }
00192                 }
00193         }
00194 
00201         void zax( const CS_MATRIX< T >& A, const CS_ARRAY< T >& x ) {
00202                 if( A.cols() != x.getSize() ) {
00203                         std::cerr << A._descr << " (A) does not agree with " << x._descr << " (x) for z=Ax!" << std::endl;
00204                         std::cerr << CS_MATRIX< T >::fDim( A ) << " X " << CS_ARRAY< T >::fDim( x ) << std::endl;
00205                         exit( !EXIT_SUCCESS );
00206                 }
00207 
00208                 if( A.rows() != getSize() ) {
00209                         std::cerr << A._descr << " (A) does not agree with " << _descr << " (z) for z=Ax!" << std::endl;
00210                         std::cerr << CS_MATRIX< T >::fDim( A ) << " X " << CS_ARRAY< T >::fDim( *this ) << std::endl;
00211                         exit( !EXIT_SUCCESS );
00212                 }
00213 
00214                 for( unsigned int i=0; i<A.rows(); i++ ) {
00215                         //preserve order of access
00216                         const T _a = A.const_access( i, 0 );
00217                         const T _x = x.const_access( 0 );
00218                         access( i ) = _a*_x;
00219                         for( unsigned int j=1; j<A.cols(); j++ ) {
00220                                 const T _a = A.const_access( i, j );
00221                                 const T _x = x.const_access( j );
00222                                 const T _z = access( i );
00223                                 access( i ) = _a * _x + _z;
00224                         }
00225                 }
00226         }
00227 
00228 };
00229 
00230 #endif
00231 

Generated on Fri Aug 15 18:12:22 2008 for Run-timeCacheSimulator by  doxygen 1.3.9.1