LAMA
/home/brandes/workspace/LAMA/src/lama/matrix/SparseMatrix.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_SPARSE_MATRIX_HPP_
00034 #define LAMA_SPARSE_MATRIX_HPP_
00035 
00036 // for dll_import
00037 #include <lama/config.hpp>
00038 
00039 // base classes
00040 #include <lama/matrix/Matrix.hpp>
00041 
00042 // others
00043 #include <lama/storage/MatrixStorage.hpp>
00044 
00045 #include <lama/DenseVector.hpp>
00046 
00047 namespace lama
00048 
00049 {
00050 
00051 // forward declarations
00052 
00053 class Vector;
00054 template<typename> class DenseMatrix;
00055 
00060 class LAMA_DLL_IMPORTEXPORT _SparseMatrix: public Matrix
00061 {
00062 public:
00063 
00066     virtual const _MatrixStorage& getLocalStorage() const = 0;
00067 
00070     virtual const _MatrixStorage& getHaloStorage() const = 0;
00071 
00074     virtual const Halo& getHalo() const = 0;
00075 
00078     _SparseMatrix() : Matrix() 
00079     {}
00080 
00083     _SparseMatrix(const IndexType numRows, const IndexType numColumns) :
00084 
00085         Matrix( numRows, numColumns )
00086     {}
00087 
00090     _SparseMatrix( DistributionPtr rowDistribution, DistributionPtr colDistribution) :
00091 
00092         Matrix( rowDistribution, colDistribution )
00093     {}
00094 
00097     _SparseMatrix( DistributionPtr distribution ) :
00098 
00099         Matrix( distribution )
00100     {}
00101 
00113     Matrix::MatrixKind getMatrixKind() const { return Matrix::SPARSE; }
00114 };
00115 
00129 template<typename T>
00130 class LAMA_DLL_IMPORTEXPORT SparseMatrix: public _SparseMatrix
00131 {
00132 
00133     friend class SpecializedJacobi;
00134 
00135 public:
00136 
00140     typedef T ValueType;
00141 
00144     static const char* typeName();
00145 
00146     virtual const char* getTypeName() const { return typeName(); }
00147 
00150     const MatrixStorage<ValueType>& getLocalStorage() const { return *mLocalData; };
00151 
00154     const MatrixStorage<ValueType>& getHaloStorage() const { return *mHaloData; };
00155 
00167     SparseMatrix( boost::shared_ptr<MatrixStorage<T> > storage );
00168 
00169     SparseMatrix( boost::shared_ptr<MatrixStorage<T> > storage,
00170                   DistributionPtr rowDist );
00171 
00172     SparseMatrix( boost::shared_ptr<MatrixStorage<T> > storage,
00173                   DistributionPtr rowDist, DistributionPtr colDist );
00174 
00177     SparseMatrix( boost::shared_ptr<MatrixStorage<T> > localData,
00178                   boost::shared_ptr<MatrixStorage<T> > haloData,
00179                   const Halo& halo,
00180                   DistributionPtr rowDist, DistributionPtr colDist );
00181 
00182     SparseMatrix( const Matrix& matrix, const bool transposeFlag = false );
00183 
00184     SparseMatrix( const Matrix& other, DistributionPtr rowDist, DistributionPtr colDist );
00185 
00190     SparseMatrix( const SparseMatrix<ValueType>& other );
00191 
00194     virtual void invert( const Matrix& other );
00195 
00196     virtual void setContext( const ContextPtr context )
00197     {
00198         setContext( context, context );
00199     }
00200 
00201     virtual void setContext( const ContextPtr localContext, const ContextPtr haloContext ) 
00202     {
00203         mLocalData->setContext( localContext );
00204         mHaloData->setContext( haloContext );
00205     }
00206 
00207     virtual ContextPtr getContextPtr() const { return mLocalData->getContextPtr(); }
00208 
00209     virtual const Context& getContext() const { return mLocalData->getContext(); }
00210 
00213     template<typename OtherValueType>
00214     void setRawDenseData( const IndexType numRows,
00215                           const IndexType numColumns,
00216                           const OtherValueType values[],
00217                           const OtherValueType eps = 0.0 );
00218 
00221     virtual void clear();
00222 
00225     virtual void allocate( const IndexType numRows, const IndexType numColumns );
00226 
00229     virtual void allocate( DistributionPtr distribution, DistributionPtr colDistribution );
00230 
00233     virtual void setIdentity();
00234 
00237     virtual void assign( const Matrix& other );
00238 
00241     void assign( const _SparseMatrix& matrix );
00242 
00253     void assign( const _MatrixStorage& other );
00254 
00273     void assign( const _MatrixStorage& storage, DistributionPtr rowDist, DistributionPtr colDist );
00274 
00277     virtual void buildLocalStorage( _MatrixStorage& storage ) const;
00278 
00286     void swap( SparseMatrix<ValueType>& other );
00287 
00291     virtual ~SparseMatrix();
00292 
00299     virtual void getDiagonal( Vector& diagonal ) const;
00300 
00301     virtual void getRow( Vector& row, const IndexType globalRowIndex ) const;
00302 
00309     virtual void setDiagonal( const Vector& diagonal );
00310 
00317     virtual void setDiagonal( const Scalar scalar );
00318 
00325     virtual void scale( const Vector& scaling );
00326 
00331     virtual void scale( const Scalar scaling );
00332 
00339     //TODO: implement or delete
00340     //void setLocal( const MatrixStorage<ValueType>& newLocalData );
00341 
00344     virtual void matrixTimesScalar(
00345         const Matrix& other,
00346         const Scalar alpha );
00347 
00348     void matrixTimesVectorImpl(
00349         DenseVector<ValueType>& result,
00350         const ValueType alpha,
00351         const DenseVector<ValueType>& x,
00352         const ValueType beta,
00353         const DenseVector<ValueType>& y ) const;
00354 
00357     virtual void matrixTimesVectorNImpl(
00358         DenseMatrix<ValueType>& result,
00359         const ValueType alpha,
00360         const DenseMatrix<ValueType>& x, 
00361         const ValueType beta,
00362         const DenseMatrix<ValueType>& y ) const;
00363 
00364     virtual void matrixTimesVector(
00365         Vector& result,
00366         const Scalar alpha,
00367         const Vector& x,
00368         const Scalar beta,
00369         const Vector& y ) const;
00370 
00373     virtual void matrixTimesMatrix(
00374         Matrix& result,
00375         const Scalar alpha,
00376         const Matrix& B,
00377         const Scalar beta,
00378         const Matrix& C ) const;
00379 
00382     Scalar maxDiffNorm( const Matrix& other ) const;
00383 
00386     ValueType maxDiffNormImpl( const SparseMatrix<ValueType>& other ) const;
00387 
00390     IndexType getLocalNumValues() const;
00391 
00392     /* Getter routine for the local number of rows and columns */
00393 
00394     IndexType getLocalNumRows() const;
00395 
00396     IndexType getLocalNumColumns() const;
00397 
00398     /* Getter routine returning the total num values of the whole distributed matrix */
00399 
00400     virtual IndexType getNumValues() const;
00401 
00402     /* Getter routine returning the num values of the local partition (local + halo) of the matrix */
00403 
00404     IndexType getPartitialNumValues() const;
00405 
00406     Scalar getValue( IndexType i, IndexType j ) const;
00407 
00410     const Halo& getHalo() const;
00411 
00412     virtual void writeAt( std::ostream& stream ) const;
00413 
00414     virtual void prefetch() const;
00415 
00416     virtual void wait() const;
00417 
00418     virtual void setComputeKind( SyncKind computeKind );
00419 
00420     virtual bool hasDiagonalProperty() const;
00421 
00422     virtual void resetDiagonalProperty();
00423 
00424     virtual Scalar::ScalarType getValueType() const;
00425 
00426     virtual std::auto_ptr<Matrix> create() const;
00427 
00430     virtual std::auto_ptr<Matrix> copy() const;
00431 
00434     void redistribute( DistributionPtr rowDistribution , DistributionPtr colDistribution );
00435 
00438     void assignTranspose ( const Matrix& matrix );
00439 
00440     virtual size_t getMemoryUsage() const;
00441 
00444     void writeToFile(
00445         const std::string& fileName,
00446         const File::FileType fileType = File::BINARY,
00447         const File::DataType dataType = File::INTERNAL,
00448         const File::IndexDataType indexDataTypeIA = File::INT,
00449         const File::IndexDataType indexDataTypeJA = File::INT ) const;
00450 
00464     void readFromFile( const std::string& filename );
00465 
00468     SparseMatrix& operator=( const SparseMatrix& matrix );
00469 
00470     SparseMatrix& operator=( const Matrix& matrix );
00471 
00472     SparseMatrix& operator=( const Expression<Matrix,Matrix,Times>& expression);
00473 
00474     SparseMatrix& operator=( const Expression<Scalar,Matrix,Times>& expression);
00475 
00476     SparseMatrix& operator=( const Expression<Scalar,Expression<Matrix,Matrix,Times>,Times>& expression);
00477 
00478 protected:
00479 
00482     void checkSettings();
00483 
00484     boost::shared_ptr<MatrixStorage<ValueType> > mLocalData; 
00485 
00486     boost::shared_ptr<MatrixStorage<ValueType> > mHaloData;  
00487 
00488     Halo mHalo; 
00489 
00492     void matrixTimesMatrixImpl( const ValueType alpha,
00493                                 const SparseMatrix<ValueType>& A,
00494                                 const SparseMatrix<ValueType>& B,
00495                                 const ValueType beta,
00496                                 const SparseMatrix<ValueType>& C ) ;
00497 private:
00498 
00502     SparseMatrix();
00503 
00504     LAMA_LOG_DECL_STATIC_LOGGER( logger );
00505 
00517     void set( const MatrixStorage<ValueType>& otherLocalData, DistributionPtr otherDist );
00518 
00521     void assignTransposeImpl ( const SparseMatrix<ValueType>& matrix );
00522 
00525     //TODO: not implemented --> implement or delete
00526     //void computeHalo( const MatrixStorage<T>& localData, const Distribution& rowDistribution );
00527 
00528     void getLocalRow( DenseVector<ValueType>& row, const IndexType iLocal ) const;
00529 
00530     mutable LAMAArray<ValueType> mTemp; 
00531 
00532     mutable LAMAArray<ValueType> mTempSendValues; 
00533 };
00534 
00535 /* ------------------------------------------------------------------------- */
00536 
00537 template<typename ValueType>
00538 template<typename OtherValueType>
00539 void SparseMatrix<ValueType>::setRawDenseData( const IndexType numRows,
00540                                                const IndexType numColumns,
00541                                                const OtherValueType values[],
00542                                                const OtherValueType eps )
00543 {
00544     Matrix::setReplicatedMatrix( numRows, numColumns );  // sets global distributions
00545 
00546     mLocalData->setRawDenseData( numRows, numColumns, values, eps );
00547     mHaloData->allocate( numRows, 0 );
00548     mHalo = Halo();
00549 }
00550 
00551 }
00552 
00553 #endif // LAMA_SPARSE_MATRIX_HPP_