LAMA
/home/brandes/workspace/LAMA/src/lama/storage/CSRStorage.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_CSRSTORAGE_HPP_
00034 #define LAMA_CSRSTORAGE_HPP_
00035 
00036 // for dll_import
00037 #include <lama/config.hpp>
00038 
00039 // base classes
00040 #include <lama/storage/CRTPMatrixStorage.hpp>
00041 
00042 namespace lama
00043 {
00044 
00077 template<typename T>
00078 class LAMA_DLL_IMPORTEXPORT CSRStorage: public CRTPMatrixStorage<CSRStorage<T>, T>
00079 {
00080 public:
00081 
00082     typedef T ValueType;    
00083 
00086     static const char* typeName();
00087 
00090     CSRStorage();
00091 
00098     CSRStorage(const IndexType numRows, const IndexType numColumns);
00099 
00102     CSRStorage( const IndexType numRows,
00103                 const IndexType numColumns,
00104                 const IndexType numValues,
00105                 const LAMAArray<IndexType>& ia,
00106                 const LAMAArray<IndexType>& ja,
00107                 const LAMAArray<ValueType>& values  );
00108 
00111     explicit CSRStorage( const _MatrixStorage& other ) {  assign( other ); }
00112 
00115     CSRStorage( const _MatrixStorage& other, const ContextPtr context )
00116     {  _MatrixStorage::setContext(context); assign( other ); }
00117     
00118     CSRStorage<ValueType>& operator=( const _MatrixStorage& other)
00119     {  assign( other );  return *this; }
00120 
00125     virtual void clear();
00126 
00129     virtual void purge();
00130 
00133     virtual CSRStorage* copy() const;
00134 
00137     virtual CSRStorage* create() const;
00138 
00141     virtual ~CSRStorage();
00142 
00148     void check( const char* msg ) const;
00149 
00152     virtual MatrixStorageFormat getFormat() const;
00153 
00156     virtual void setIdentity(const IndexType size);
00157 
00169     template<typename OtherValueType>
00170     void setCSRDataImpl( const IndexType numRows,
00171                          const IndexType numColumns,
00172                          const IndexType numValues,
00173                          const LAMAArray<IndexType>& ia,
00174                          const LAMAArray<IndexType>& ja,
00175                          const LAMAArray<OtherValueType>& values,
00176                          const ContextPtr loc );
00177 
00189     template<typename OtherValueType>
00190     void setCSRDataSwap( const IndexType numRows,
00191                          const IndexType numColumns,
00192                          const IndexType numValues,
00193                                LAMAArray<IndexType>& ia,
00194                                LAMAArray<IndexType>& ja,
00195                                LAMAArray<OtherValueType>& values,
00196                          const ContextPtr loc );
00197                         
00198 
00201     virtual void writeAt(std::ostream& stream) const;
00202 
00205     LAMAArray<IndexType>& getIA();
00206 
00209     LAMAArray<IndexType>& getJA();
00210 
00213     LAMAArray<ValueType>& getValues();
00214 
00215     const LAMAArray<IndexType>& getIA() const;
00216 
00217     const LAMAArray<IndexType>& getJA() const;
00218 
00219     const LAMAArray<ValueType>& getValues() const;
00220 
00223     IndexType getNumValues() const;
00224 
00227     template<typename OtherType>
00228     void getRowImpl( LAMAArray<OtherType>& row, const IndexType i ) const;
00229 
00234     template<typename OtherValueType>
00235     void getDiagonalImpl( LAMAArray<OtherValueType>& diagonal ) const;
00236 
00241     template<typename OtherValueType>
00242     void setDiagonalImpl( const LAMAArray<OtherValueType>& diagonal );
00243 
00246     void setDiagonalImpl( const Scalar scalar );
00247 
00248     /******************************************************************
00249     *  Scaling of elements in a matrix                                *
00250     ******************************************************************/
00251 
00254     template<typename OtherType>
00255     void scaleImpl( const LAMAArray<OtherType>& values );
00256 
00259     void scaleImpl( const Scalar value );
00260 
00269     T getValue(IndexType i, IndexType j) const;
00270 
00273     virtual void prefetch( const ContextPtr location ) const;
00274 
00277     void wait() const;
00278 
00279     using MatrixStorage<ValueType>::mDiagonalProperty;
00280     using MatrixStorage<ValueType>::mRowIndexes;
00281     using MatrixStorage<ValueType>::mCompressThreshold;
00282 
00283     using MatrixStorage<ValueType>::prefetch;
00284     using MatrixStorage<ValueType>::getContext;
00285     using MatrixStorage<ValueType>::getContextPtr;
00286 
00287     IndexType mNumValues; 
00288 
00289     LAMAArray<IndexType> mIa; 
00290     LAMAArray<IndexType> mJa; 
00291     LAMAArray<ValueType> mValues; 
00292 
00298     void allocate(const IndexType numRows, const IndexType numColumns );
00299 
00311     void compress( const ValueType eps = 0.0 );
00312 
00320     void swap( CSRStorage<ValueType>& other );
00321 
00328     void swap( LAMAArray<IndexType>& ia, LAMAArray<IndexType>& ja, LAMAArray<ValueType>& values );
00329 
00332     virtual size_t getMemoryUsageImpl() const;
00333 
00338     virtual void assignTranspose( const MatrixStorage<ValueType>& other );
00339 
00344     virtual void assign( const _MatrixStorage& other );
00345 
00349     virtual void copyTo( _MatrixStorage& other ) const;
00350 
00359     template<typename OtherValueType>
00360     void buildCSR( LAMAArray<IndexType>& ia,
00361                    LAMAArray<IndexType>* ja,
00362                    LAMAArray<OtherValueType>* values,
00363                    const ContextPtr /* loc */ ) const;
00364 
00370     virtual void buildCSCData( LAMAArray<IndexType>& colIA,
00371                                LAMAArray<IndexType>& colJA,
00372                                LAMAArray<ValueType>& cscValues ) const;
00373 
00376     virtual void matrixTimesVector(
00377         LAMAArrayView<ValueType> result,
00378         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00379         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00380 
00381     virtual void matrixTimesVectorN(
00382         LAMAArrayView<ValueType> result,
00383         const IndexType n,
00384         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00385         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00386 
00389     virtual std::auto_ptr<SyncToken> matrixTimesVectorAsync(
00390         LAMAArrayView<ValueType> result,
00391         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00392         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00393 
00396     virtual void matrixTimesMatrix(
00397         const ValueType alpha, 
00398         const MatrixStorage<ValueType>& a,
00399         const MatrixStorage<ValueType>& b,
00400         const ValueType beta, 
00401         const MatrixStorage<ValueType>& c );
00402 
00405     virtual void jacobiIterate(
00406         LAMAArrayView<ValueType> solution, const LAMAArrayConstView<ValueType> oldSolution,
00407         const LAMAArrayConstView<ValueType> rhs, const ValueType omega) const;
00408 
00409     virtual void jacobiIterateHalo(
00410         LAMAArrayView<ValueType> localSolution, 
00411         const MatrixStorage<ValueType>& localStorage, 
00412         const LAMAArrayConstView<ValueType> haloOldSolution,
00413         const ValueType omega ) const;
00414 
00417     virtual ValueType maxDiffNorm ( const MatrixStorage<ValueType>& other );
00418 
00421     virtual ValueType maxDiffNormImpl ( const CSRStorage<ValueType>& other );
00422 
00425     void print() const;
00426 
00427 protected:
00428 
00429     using MatrixStorage<ValueType>::mNumRows;
00430     using MatrixStorage<ValueType>::mNumColumns;
00431 
00432 private:
00433 
00434     bool mSortedRows;  
00435 
00439     virtual bool checkDiagonalProperty() const;
00440 
00446     void buildRowIndexes( );
00447 
00448     LAMA_LOG_DECL_STATIC_LOGGER( logger ); 
00449 
00452     void matrixAddMatrixCSR( const ValueType alpha, 
00453                              const CSRStorage<ValueType>& a,
00454                              const ValueType beta,
00455                              const CSRStorage<ValueType>& b );
00456 
00459     void matrixTimesMatrixCSR( const ValueType alpha, 
00460                                const CSRStorage<ValueType>& a,
00461                                const CSRStorage<ValueType>& b );
00462 };
00463 
00464 /* --------------------------------------------------------------------------- */
00465 
00466 }
00467 #endif // LAMA_CSRSTORAGE_HPP_