LAMA
|
00001 00033 #ifndef LAMA_MATRIX_HPP_ 00034 #define LAMA_MATRIX_HPP_ 00035 00036 // for dll_import 00037 #include <lama/config.hpp> 00038 00039 // base classes 00040 #include <lama/Printable.hpp> 00041 #include <lama/Distributed.hpp> 00042 00043 // others 00044 #include <lama/LAMATypes.hpp> 00045 #include <lama/Scalar.hpp> 00046 #include <lama/Vector.hpp> 00047 #include <lama/Context.hpp> 00048 00049 #include <lama/distribution/Distribution.hpp> 00050 00051 // logging 00052 #include <logging/logging.hpp> 00053 00054 namespace lama 00055 { 00056 00057 // Forward declaration 00058 00059 class _MatrixStorage; 00060 00063 typedef boost::shared_ptr<class Matrix> MatrixPtr; 00064 00071 class LAMA_DLL_IMPORTEXPORT Matrix : public Printable, 00072 public Distributed 00073 { 00074 00075 public: 00076 00080 typedef const Matrix& ExpressionMemberType; 00081 00097 Matrix( DistributionPtr rowDistribution, DistributionPtr colDistribution ); 00098 00107 Matrix( const IndexType numRows, const IndexType numColumns ); 00108 00117 Matrix( DistributionPtr distribution ); 00118 00126 explicit Matrix( const IndexType size ); 00127 00131 Matrix(); 00132 00148 Matrix( const Matrix& other, DistributionPtr rowDistribution, DistributionPtr colDistribution ); 00149 00157 Matrix( const Matrix& other ); 00158 00162 virtual ~Matrix(); 00163 00168 virtual const char* getTypeName() const = 0; 00169 00181 std::auto_ptr<_LAMAArray> createArray() const; 00182 00194 virtual void clear() = 0; 00195 00205 virtual void allocate( const IndexType numRows, const IndexType numColumns ) = 0; 00206 00215 virtual void allocate( DistributionPtr rowDistribution, DistributionPtr colDistribution ) = 0; 00216 00227 virtual void setIdentity() = 0; 00228 00235 virtual void assign( const Matrix& other ) = 0; 00236 00242 virtual void assign( const _MatrixStorage& other ) = 0; 00243 00246 virtual void assign( const _MatrixStorage& storage, DistributionPtr rowDist, DistributionPtr colDist ) = 0; 00247 00257 virtual void buildLocalStorage( _MatrixStorage& storage ) const = 0; 00258 00265 virtual void redistribute( DistributionPtr rowDistribution , DistributionPtr colDistribution ) = 0; 00266 00276 virtual void getRow( Vector& row, const IndexType globalRowIndex ) const = 0; 00277 00284 virtual void getDiagonal( Vector& diagonal ) const = 0; 00285 00292 virtual void setDiagonal( const Vector& diagonal ) = 0; 00293 00300 virtual void setDiagonal( const Scalar scalar ) = 0; 00301 00308 virtual void scale( const Vector& scaling ) = 0; 00309 00314 virtual void scale( const Scalar scaling ) = 0; 00315 00325 Scalar operator()( IndexType i, IndexType j ) const; 00326 00336 virtual Scalar getValue( IndexType i, IndexType j ) const = 0; 00337 00343 virtual void writeAt( std::ostream& stream ) const; 00344 00350 inline IndexType getNumRows() const; 00351 00357 inline IndexType getNumColumns() const; 00358 00370 virtual IndexType getNumValues() const = 0; 00371 00372 /* Returns the sparsity rate (#nnz/(#rows*#columns)) of the whole matrix */ 00373 00374 double getSparsityRate() const; 00375 00388 virtual void matrixTimesVector( 00389 Vector& result, const Scalar alpha, const Vector& x, 00390 const Scalar beta, const Vector& y ) const = 0; 00391 00400 virtual void matrixTimesScalar( 00401 const Matrix& other, const Scalar alpha ) = 0; 00402 00414 virtual void matrixTimesMatrix( 00415 Matrix& result, const Scalar alpha, const Matrix& B, 00416 const Scalar beta, const Matrix& C ) const = 0; 00417 00427 virtual void matrix2CSRGraph( IndexType* xadj, IndexType* adjncy, IndexType* vwgt, CommunicatorPtr comm, 00428 const IndexType* globalRowIndices = NULL, IndexType* vtxdist = NULL ) const; 00429 00432 virtual IndexType getLocalNumValues() const = 0; 00433 00436 virtual IndexType getLocalNumRows() const = 0; 00437 00440 virtual IndexType getLocalNumColumns() const = 0; 00441 00447 inline const Distribution& getColDistribution() const; 00448 00454 inline DistributionPtr getColDistributionPtr() const; 00455 00464 virtual void setContext( const ContextPtr context ) = 0; 00465 00470 virtual void setContext( const ContextPtr localContext, const ContextPtr haloContext); 00471 00477 virtual ContextPtr getContextPtr() const = 0; 00478 00479 const Context& getContext() const { return *getContextPtr(); } 00480 00484 typedef enum 00485 { 00486 ASYNCHRONOUS, 00487 SYNCHRONOUS 00488 } SyncKind; 00489 00493 typedef enum 00494 { 00495 DENSE, 00496 SPARSE 00497 } MatrixKind; 00498 00501 virtual MatrixKind getMatrixKind() const = 0; 00502 00508 inline SyncKind getCommunicationKind() const; 00509 00515 inline SyncKind getComputeKind() const; 00516 00522 void setCommunicationKind( SyncKind communicationKind ); 00523 00529 virtual void setComputeKind( SyncKind computeKind ); 00530 00537 void inheritAttributes( const Matrix& other ); 00538 00542 virtual void prefetch() const = 0; 00543 00547 virtual void wait() const = 0; 00548 00558 Matrix& operator=( const Matrix& other ); 00559 00563 Matrix& operator=( Expression<Scalar, Matrix, Times> exp ); 00564 00568 Matrix& operator=( Expression<Matrix, Matrix, Times> exp ); 00569 00573 Matrix& operator=( Expression<Scalar, Expression<Matrix, Matrix, Times>, Times> exp ); 00574 00578 Matrix& operator=( Expression< Expression< Scalar, Expression< Matrix, Matrix, Times>, Times>, 00579 Expression< Scalar, Matrix, Times>, Plus> exp ); 00580 00583 virtual void invert( const Matrix& other ) = 0; 00584 00594 virtual Scalar maxDiffNorm( const Matrix& other ) const =0; 00595 00620 virtual std::auto_ptr<Matrix> create() const = 0; 00621 00625 std::auto_ptr<Matrix> create( const IndexType numRows, const IndexType numColumns ) const; 00626 00630 std::auto_ptr<Matrix> create( const IndexType size ) const; 00631 00635 std::auto_ptr<Matrix> create( DistributionPtr rowDistribution, DistributionPtr colDistribution ) const; 00636 00640 std::auto_ptr<Matrix> create( DistributionPtr distribution ) const; 00641 00645 VectorPtr createDenseVector( DistributionPtr distribution, const Scalar value ) const; 00646 00659 virtual std::auto_ptr<Matrix> copy() const = 0; 00660 00664 virtual Scalar::ScalarType getValueType() const = 0; 00665 00670 virtual bool hasDiagonalProperty() const = 0; 00671 00676 virtual void resetDiagonalProperty() = 0; 00677 00686 virtual size_t getMemoryUsage() const = 0; 00687 00688 protected: 00689 00696 void setReplicatedMatrix( const IndexType numRows, const IndexType numColumns ); 00697 00706 void setDistributedMatrix( DistributionPtr distribution, DistributionPtr colDistribution ); 00707 00708 DistributionPtr mColDistribution; 00709 00710 //TODO: remove mNumRows and mNumColumns, this value is stored in the distribution 00711 IndexType mNumRows; 00712 IndexType mNumColumns; 00713 00714 protected: 00715 00716 void checkSettings(); // check valid member variables 00717 00718 void swapMatrix( Matrix& other ); // swap member variables of Matrix 00719 00720 LAMA_LOG_DECL_STATIC_LOGGER( logger ); 00721 00722 private: 00723 00724 void setDefaultKind(); // set default values for communication and compute kind 00725 00726 SyncKind mCommunicationKind; 00727 SyncKind mComputeKind; 00728 }; 00729 00730 /* ======================================================================== */ 00731 /* Inline methods */ 00732 /* ======================================================================== */ 00733 00734 inline IndexType Matrix::getNumRows() const 00735 { 00736 //return getDistributionPtr().get()->getGlobalSize(); 00737 return mNumRows; 00738 } 00739 00740 inline IndexType Matrix::getNumColumns() const 00741 { 00742 //return getColDistributionPtr().get()->getGlobalSize(); 00743 return mNumColumns; 00744 } 00745 00746 inline Matrix::SyncKind Matrix::getCommunicationKind() const 00747 { 00748 return mCommunicationKind; 00749 } 00750 00751 inline Matrix::SyncKind Matrix::getComputeKind() const 00752 { 00753 return mComputeKind; 00754 } 00755 00756 inline const Distribution& Matrix::getColDistribution() const 00757 { 00758 LAMA_ASSERT_ERROR( mColDistribution, "NULL column distribution for Matrix" ); 00759 return *mColDistribution; 00760 } 00761 00762 inline DistributionPtr Matrix::getColDistributionPtr() const 00763 { 00764 LAMA_ASSERT_ERROR( mColDistribution, "NULL column distribution for Matrix" ); 00765 return mColDistribution; 00766 } 00767 00773 inline std::ostream& operator<<( std::ostream& stream, const Matrix::SyncKind& kind ) 00774 { 00775 switch ( kind ) 00776 { 00777 case Matrix::SYNCHRONOUS: 00778 { 00779 stream << "SYNCHRONOUS"; 00780 break; 00781 } 00782 case Matrix::ASYNCHRONOUS: 00783 { 00784 stream << "ASYNCHRONOUS"; 00785 break; 00786 } 00787 default: 00788 { 00789 stream << "<unknown sync kind>"; 00790 break; 00791 } 00792 } 00793 00794 return stream; 00795 } 00796 00797 } 00798 00799 #endif // LAMA_MATRIX_HPP_