LAMA
/home/brandes/workspace/LAMA/src/lama/storage/MatrixStorage.hpp
Go to the documentation of this file.
00001 
00034 #ifndef LAMA_MATRIX_STORAGE_HPP_
00035 #define LAMA_MATRIX_STORAGE_HPP_
00036 
00037 #include <ostream>
00038 
00039 // others
00040 #include <lama/LAMAArray.hpp>
00041 
00042 #include <lama/io/FileType.hpp>
00043 
00044 namespace lama
00045 {
00046 
00047 class Distribution;  // Forward declaration
00048 
00051 typedef enum
00052 {
00053   CSR,
00054   ELL,
00055   DIA,
00056   JDS,
00057   COO,
00058   DENSE,
00059   ASSEMBLY,
00060   UNDEFINED
00061 } MatrixStorageFormat;
00062 
00063 std::ostream& operator<< ( std::ostream& stream, 
00064                            const MatrixStorageFormat storageFormat );
00065 
00082 class LAMA_DLL_IMPORTEXPORT _MatrixStorage : public Printable
00083 {
00084 public:
00085 
00088     _MatrixStorage( const IndexType numRows, const IndexType numColumns);
00089 
00092     _MatrixStorage();
00093 
00094     virtual ~_MatrixStorage();
00095 
00101     virtual const char* getTypeName() const = 0;
00102 
00105     void init( const IndexType numRows, const IndexType numColumns );
00106 
00109     virtual Scalar::ScalarType getValueType() const = 0;
00110 
00116     virtual void clear() = 0;
00117 
00121     virtual void purge() = 0;
00122 
00131     virtual void allocate( const IndexType numRows, const IndexType numColumns) = 0;
00132 
00133     /******************************************************************
00134     *  mContext  : setter / getter for preferred context              *
00135     ******************************************************************/
00136  
00142     void setContext( ContextPtr context );
00143 
00146     ContextPtr getContextPtr() const { return mContext; }
00147 
00150     const Context& getContext() const { return *mContext; }
00151 
00154     virtual void prefetch( const ContextPtr context ) const = 0;
00155 
00158     void prefetch() const { prefetch( mContext ); }
00159 
00162     virtual void wait() const = 0;
00163 
00166     void setCompressThreshold(float ratio);
00167 
00168     inline IndexType getNumRows() const;
00169 
00170     inline IndexType getNumColumns() const;
00171 
00172     virtual void writeAt(std::ostream& stream) const;
00173 
00174     void resetDiagonalProperty();
00175 
00176     inline bool hasDiagonalProperty() const;
00177 
00178     virtual MatrixStorageFormat getFormat() const = 0;
00179 
00185     virtual void setIdentity( const IndexType n ) = 0;
00186 
00194     virtual void getRow( _LAMAArray& row, const IndexType i ) const = 0;
00195 
00204     virtual void getDiagonal( _LAMAArray& diagonal ) const = 0;
00205 
00210     virtual void setDiagonal( const _LAMAArray& diagonal ) = 0;
00211 
00212     virtual void setDiagonal( const Scalar value ) = 0;
00213 
00214     /******************************************************************
00215     *  Scaling of elements in a matrix                                *
00216     ******************************************************************/
00217  
00222     virtual void scale( const Scalar value ) = 0;
00223 
00230     virtual void scale( const _LAMAArray& values ) = 0;
00231 
00232     /******************************************************************
00233     *  General operations on a matrix                                 *
00234     ******************************************************************/
00235  
00246     virtual void localize( const _MatrixStorage& global,
00247                            const Distribution& rowDist );
00248 
00267     virtual IndexType getNumValues() const;
00268 
00269     inline const LAMAArray<IndexType>& getRowIndexes() const;
00270 
00287     virtual void buildCSRSizes( LAMAArray<IndexType>& csrIA ) const = 0;
00288 
00311     virtual void buildCSRData( LAMAArray<IndexType>& csrIA,
00312                                LAMAArray<IndexType>& csrJA,
00313                                _LAMAArray& csrValues ) const = 0;
00314 
00325     virtual void setCSRData( const IndexType numRows,
00326                              const IndexType numColumns,
00327                              const IndexType numValues,
00328                              const LAMAArray<IndexType>& csrIA,
00329                              const LAMAArray<IndexType>& csrJA,
00330                              const _LAMAArray& csrValues ) = 0;
00331 
00339     virtual void assign( const _MatrixStorage& other ) = 0;
00340 
00344     virtual void copyTo( _MatrixStorage& other ) const = 0;
00345 
00348     _MatrixStorage& operator=( const _MatrixStorage& other );
00349 
00350     /******************************************************************
00351     *   Help routines (ToDo: -> LAMAArrayUtils ?? )                   *
00352     ******************************************************************/
00353  
00356     static void offsets2sizes( LAMAArray<IndexType>& offsets );
00357 
00358     static void offsets2sizes( LAMAArray<IndexType>& sizes, const LAMAArray<IndexType>& offsets );
00359 
00362     static IndexType sizes2offsets( LAMAArray<IndexType>& sizes );
00363 
00371     /******************************************************************
00372     *   Query routines                                                *
00373     ******************************************************************/
00374  
00375     size_t getMemoryUsage() const;
00376 
00377     virtual void check( const char* msg ) const = 0;
00378 
00390     virtual _MatrixStorage* create() const = 0;
00391 
00403     virtual _MatrixStorage* copy() const = 0;
00404 
00405 protected:
00406 
00414     void swap( _MatrixStorage& other );
00415 
00416     virtual void _assignTranspose( const _MatrixStorage& other );
00417 
00418     virtual void _assign( const _MatrixStorage& other );
00419 
00430     virtual size_t getMemoryUsageImpl() const =0;
00431 
00432     IndexType mNumRows;
00433 
00434     IndexType mNumColumns;
00435 
00436     LAMAArray<IndexType> mRowIndexes; 
00437 
00438     float mCompressThreshold;   
00439 
00440     bool mDiagonalProperty;  
00441 
00442     LAMA_LOG_DECL_STATIC_LOGGER( logger );  
00443 
00444     ContextPtr mContext;        
00445 
00446 private:
00447 
00453     virtual bool checkDiagonalProperty() const = 0;
00454 };
00455 
00456 class Distribution;
00457 
00464 template<typename T>
00465 class LAMA_DLL_IMPORTEXPORT MatrixStorage : public _MatrixStorage
00466 {
00467 public:
00468 
00471     typedef T ValueType;
00472 
00475     MatrixStorage( const IndexType numRows,
00476                    const IndexType numColumns);
00477 
00480     MatrixStorage();
00481 
00484     virtual ~MatrixStorage();
00485 
00488     virtual MatrixStorage* create() const = 0;
00489 
00492     virtual MatrixStorage* copy() const = 0;
00493 
00496     virtual Scalar::ScalarType getValueType() const;
00497 
00509     template<typename OtherValueType>
00510     void setRawDenseData( const IndexType numRows,
00511                           const IndexType numColumns,
00512                           const OtherValueType values[],
00513                           const ValueType eps = 0.0 );
00514 
00526     template<typename OtherValueType>
00527     void setRawCSRData( const IndexType numRows,
00528                         const IndexType numColumns,
00529                         const IndexType numValues,
00530                         const IndexType* const ia,
00531                         const IndexType* const ja,
00532                         const OtherValueType* const values);
00533 
00550     virtual void joinHalo( const _MatrixStorage& localData,
00551                            const _MatrixStorage& haloData,
00552                            const class Halo& halo, 
00553                            const class Distribution& colDist );
00554 
00564     virtual void splitHalo( MatrixStorage<ValueType>& localData,
00565                             MatrixStorage<ValueType>& haloData,
00566                             class Halo& halo,
00567                             const class Distribution& colDist,
00568                             const class Distribution* rowDist ) const;
00569 
00578     virtual void buildHalo( class Halo& halo,
00579                             const class Distribution& colDist );
00580 
00589     virtual void localize( const _MatrixStorage& globalData, 
00590                            const class Distribution& rowDist );
00591     
00599     virtual void replicate( const _MatrixStorage& localData,
00600                             const class Distribution& rowDist );
00601 
00609     virtual ValueType getValue( IndexType i, IndexType j ) const = 0;
00610 
00616     virtual void buildCSCData( LAMAArray<IndexType>& cscIA,
00617                                LAMAArray<IndexType>& cscJA,
00618                                LAMAArray<ValueType>& cscValues ) const;
00619 
00623     virtual void assign( const _MatrixStorage& other );
00624 
00628     virtual void copyTo( _MatrixStorage& other ) const;
00629 
00635     virtual void assignTranspose( const MatrixStorage<ValueType>& other );
00636 
00644     virtual void redistribute( const _MatrixStorage& other, const class Redistributor& redistributor );
00645 
00648     virtual void exchangeHalo( const class Halo& halo, const MatrixStorage<ValueType>& matrix,
00649                                const class Communicator& comm );
00650 
00653     static void convertCSR2CSC( LAMAArray<IndexType>& colIA,
00654                                 LAMAArray<IndexType>& colJA,
00655                                 LAMAArray<ValueType>& colValues,
00656                                 const IndexType numColumns,
00657                                 const LAMAArray<IndexType>& rowIA,
00658                                 const LAMAArray<IndexType>& rowJA,
00659                                 const LAMAArray<ValueType>& rowValues,
00660                                 const ContextPtr loc );
00666     static void joinRows( LAMAArray<IndexType>& outIA,
00667                           LAMAArray<IndexType>& outJA,
00668                           LAMAArray<ValueType>& outValues,
00669                           const IndexType numRows,
00670                           const LAMAArray<IndexType>& rowIndexes,
00671                           const LAMAArray<IndexType>& inIA,
00672                           const LAMAArray<IndexType>& inJA,
00673                           const LAMAArray<ValueType>& inValues );
00674 
00677     MatrixStorage& operator=( const _MatrixStorage& other );
00678 
00688     /******************************************************************
00689     *   File I/O for MatrixStorage                                    *
00690     ******************************************************************/
00691  
00692     virtual void writeToFile( const std::string& fileName,
00693                               const File::FileType fileType = File::BINARY,
00694                               const File::DataType dataType = File::INTERNAL,
00695                               const File::IndexDataType indexDataTypeIA = File::INT, 
00696                               const File::IndexDataType indexDataTypeJA = File::INT ) const;
00697 
00698     virtual void writeToFile( const PartitionId size,
00699                               const PartitionId rank,
00700                               const std::string& fileName,
00701                               const File::FileType fileType = File::BINARY,
00702                               const File::DataType dataType = File::INTERNAL,
00703                               const File::IndexDataType indexDataTypeIA = File::INT, 
00704                               const File::IndexDataType indexDataTypeJA = File::INT ) const;
00705 
00706     virtual void readFromFile( const std::string& fileName );
00707 
00708     /******************************************************************
00709     *   invert                                                        *
00710     ******************************************************************/
00711 
00718     virtual void invert( const MatrixStorage<ValueType>& other );
00719  
00720     /******************************************************************
00721     *   Matrix * ( Vector | Matrix )                                  *
00722     ******************************************************************/
00723  
00730     virtual void matrixTimesVector(
00731         LAMAArrayView<ValueType> result,
00732         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00733         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00734 
00735     virtual void matrixTimesVectorN(
00736         LAMAArrayView<ValueType> result,
00737         const IndexType n,
00738         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00739         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00740 
00750     virtual std::auto_ptr<SyncToken> matrixTimesVectorAsync(
00751         LAMAArrayView<ValueType> result,
00752         const ValueType alpha, const LAMAArrayConstView<ValueType> x,
00753         const ValueType beta, const LAMAArrayConstView<ValueType> y ) const;
00754 
00760     virtual void matrixTimesScalar(
00761         const ValueType alpha, 
00762         const MatrixStorage<ValueType>& a );
00763 
00766     virtual void matrixTimesMatrix(
00767         const ValueType alpha, 
00768         const MatrixStorage<ValueType>& a,
00769         const MatrixStorage<ValueType>& b,
00770         const ValueType beta, 
00771         const MatrixStorage<ValueType>& c );
00772 
00777     virtual ValueType maxDiffNorm ( const MatrixStorage<ValueType>& other );
00778 
00779     /******************************************************************
00780     *   Solver methods (e.g. Jacobi )                                 *
00781     ******************************************************************/
00782  
00795     virtual void jacobiIterate(
00796         LAMAArrayView<ValueType> solution, const LAMAArrayConstView<ValueType> oldSolution,
00797         const LAMAArrayConstView<ValueType> rhs, const ValueType omega) const;
00798 
00801     virtual std::auto_ptr<SyncToken> jacobiIterateAsync(
00802         LAMAArrayView<ValueType> solution, const LAMAArrayConstView<ValueType> oldSolution,
00803         const LAMAArrayConstView<ValueType> rhs, const ValueType omega) const;
00804 
00818     virtual void jacobiIterateHalo(
00819         LAMAArrayView<ValueType> localSolution, 
00820         const MatrixStorage<ValueType>& localStorage, 
00821         const LAMAArrayConstView<ValueType> haloOldSolution,
00822         const ValueType omega ) const;
00823    
00824     using _MatrixStorage::getContext;
00825     using _MatrixStorage::getContextPtr;
00826 
00827 protected:
00828 
00835     ValueType mEpsilon;
00836 
00852     void swap( MatrixStorage<ValueType>& other );
00853 };
00854 
00855 /* ------------------------------------------------------------------------- */
00856 /* ------------------------------------------------------------------------- */
00857 /* ------------------------------------------------------------------------- */
00858 
00859 IndexType _MatrixStorage::getNumRows() const
00860 {
00861     return mNumRows;
00862 }
00863 
00864 IndexType _MatrixStorage::getNumColumns() const
00865 {
00866     return mNumColumns;
00867 }
00868 
00869 bool _MatrixStorage::hasDiagonalProperty() const
00870 {
00871     return mDiagonalProperty;
00872 }
00873 
00874 const LAMAArray<IndexType>& _MatrixStorage::getRowIndexes() const
00875 {
00876     return mRowIndexes;
00877 }
00878 
00879 /* ------------------------------------------------------------------------- */
00880 
00881 template<typename ValueType>
00882 template<typename OtherValueType>
00883 void MatrixStorage<ValueType>::setRawCSRData( const IndexType numRows,
00884                                               const IndexType numColumns,
00885                                               const IndexType numValues,
00886                                               const IndexType* const ia,
00887                                               const IndexType* const ja,
00888                                               const OtherValueType* const values)
00889 {
00890     // wrap the pointer data into LAMA arrays ( without copies )
00891 
00892     LAMAArrayRef<IndexType> csrIA( ia, numRows + 1 );
00893     LAMAArrayRef<IndexType> csrJA( ja, numValues );
00894     LAMAArrayRef<OtherValueType> csrValues( values, numValues );
00895 
00896     // now set the data on the context of this storage via virtual method
00897 
00898     setCSRData( numRows, numColumns, numValues, csrIA, csrJA, csrValues );
00899 }
00900 
00901 } // namespace lama
00902 
00903 #endif // LAMA_MATRIX_STORAGE_HPP_