LAMA
|
00001 00033 #ifndef LAMA_DENSEMATRIX_HPP_ 00034 #define LAMA_DENSEMATRIX_HPP_ 00035 00036 #include <boost/lexical_cast.hpp> 00037 #include <boost/shared_ptr.hpp> 00038 00039 #include <lama/config.hpp> 00040 00041 #include <lama/matrix/CRTPMatrix.hpp> 00042 #include <lama/matrix/SparseMatrix.hpp> 00043 00044 #include <lama/storage/DenseStorage.hpp> 00045 00046 namespace lama 00047 { 00048 00049 template<typename T> class DenseVector; // forward declaration 00050 00060 template<typename T> 00061 class LAMA_DLL_IMPORTEXPORT DenseMatrix: public CRTPMatrix<DenseMatrix<T>, T > 00062 { 00063 00064 public: 00065 00066 typedef T ValueType; 00067 00068 typedef boost::shared_ptr<DenseStorage<T> > DenseStoragePtr; 00069 00072 static const char* typeName(); 00073 00076 DenseMatrix(); 00077 00080 DenseMatrix( const IndexType numRows, const IndexType numColumns ); 00081 00084 DenseMatrix( DistributionPtr rowDist, DistributionPtr colDist ); 00085 00088 explicit DenseMatrix( DistributionPtr dist ); 00089 00096 DenseMatrix( const DenseMatrix<ValueType>& other ); 00097 00103 DenseMatrix( const Matrix& other ); 00104 00120 DenseMatrix( const Matrix& other, DistributionPtr distribution, DistributionPtr colDistribution ); 00121 00122 DenseMatrix( const DenseMatrix<ValueType>& matrix, DistributionPtr distribution, DistributionPtr colDistribution ); 00123 00134 DenseMatrix( const _MatrixStorage& localData, DistributionPtr rowDist, DistributionPtr colDist ); 00135 00146 template<typename OtherValueType> 00147 DenseMatrix( 00148 const IndexType numRows, 00149 const IndexType numColumns, 00150 const IndexType numNoneZeros, 00151 const IndexType* const ia, 00152 const IndexType* const ja, 00153 const OtherValueType* const values ); 00154 00155 DenseMatrix( const Expression<Expression<Scalar,Expression<Matrix,Matrix,Times>,Times>,Expression<Scalar,Matrix,Times>,Plus> expression ); 00156 00157 DenseMatrix( const Expression<Matrix, Matrix, Times> expression ); 00158 00159 DenseMatrix( const Expression<Scalar,Expression<Matrix,Matrix,Times>,Times> expression ); 00160 00161 DenseMatrix( const Expression<Scalar,Matrix,Times> expression ); 00162 00171 DenseMatrix( const std::string& filename ); 00172 00173 virtual ~DenseMatrix(); 00174 00177 DenseMatrix& operator=( const DenseMatrix& matrix ); 00178 00179 DenseMatrix& operator=( const Matrix& matrix ); 00180 00181 DenseMatrix& operator=( const Expression<Scalar,Matrix,Times> expression ); 00182 00183 DenseMatrix& operator=( const Expression<Matrix,Matrix,Times> expression ); 00184 00185 DenseMatrix& operator=( const Expression<Scalar,Expression<Matrix,Matrix,Times>,Times> expression ); 00186 00187 DenseMatrix& operator=( const Expression<Expression<Scalar,Expression<Matrix,Matrix,Times>,Times>,Expression<Scalar,Matrix,Times>,Plus> expression ); 00188 00191 const char* getTypeName() const; 00192 00193 Matrix::MatrixKind getMatrixKind() const { return Matrix::DENSE; } 00194 00195 virtual void setContext( const ContextPtr context ); 00196 00197 using CRTPMatrix<DenseMatrix<T>, T >::setContext; // setContext( localContext, haloContext ) 00198 00199 virtual ContextPtr getContextPtr() const { return mData[0]->getContextPtr(); } 00200 00201 virtual void setIdentity(); 00202 00205 template<typename OtherValueType> 00206 void setRawDenseData( const IndexType numRows, 00207 const IndexType numColumns, 00208 const OtherValueType values[], 00209 const OtherValueType eps = 0.0 ); 00210 00213 virtual void buildCSRData( LAMAArray<IndexType>& rowIA, 00214 LAMAArray<IndexType>& rowJA, 00215 _LAMAArray& rowValues ) const; 00216 00219 virtual void setCSRData( const LAMAArray<IndexType>& rowIA, 00220 const LAMAArray<IndexType>& rowJA, 00221 const _LAMAArray& rowValues, 00222 DistributionPtr rowDistribution, 00223 DistributionPtr colDistribution ); 00224 00227 void setCSRDataLocal( const LAMAArray<IndexType>& rowIA, 00228 const LAMAArray<IndexType>& rowJA, 00229 const _LAMAArray& rowValues ) const; 00230 00233 virtual void clear(); 00234 00237 virtual void allocate( const IndexType numRows, const IndexType numColumns ); 00238 00241 virtual void allocate( DistributionPtr rowDistribution, DistributionPtr colDistribution ); 00242 00249 virtual void assign( const Matrix& other ); 00250 00258 void swap( DenseMatrix<ValueType>& other ); 00259 00260 void assignSparse( const _SparseMatrix& other ); 00261 00264 void assign( const _MatrixStorage& storage ); 00265 00268 void assign( const _MatrixStorage& storage, DistributionPtr rowDist, DistributionPtr colDist ); 00269 00270 void assignLocal( const _MatrixStorage& other ); 00271 00274 virtual void buildLocalStorage( _MatrixStorage& storage ) const; 00275 00278 void redistribute( DistributionPtr rowDistribution , DistributionPtr colDistribution ); 00279 00280 virtual void getDiagonal( Vector& diagonal ) const; 00281 virtual void getRow( Vector& row, const IndexType globalRowIndex ) const; 00282 virtual void setDiagonal( const Vector& diagonal ); 00283 virtual void setDiagonal( const Scalar diagonalValue ); 00284 virtual void scale( const Vector& values ); 00285 virtual void scale( const Scalar value ); 00286 00287 Scalar getValue(lama::IndexType i, lama::IndexType j) const; 00288 00289 void matrixTimesScalar( const Matrix& other, const Scalar alpha); 00290 00299 void matrixTimesVectorImpl( DenseVector<ValueType>& denseResult, 00300 const ValueType alphaValue, 00301 const DenseVector<ValueType>& denseX, 00302 const ValueType betaValue, 00303 const DenseVector<ValueType>& denseY ) const; 00304 00305 /****************************************************************** 00306 * invert * 00307 ******************************************************************/ 00308 00314 virtual void invert( const Matrix& other ); 00315 00318 void invert() { this->invert( *this ); } 00319 00322 Scalar maxDiffNorm( const Matrix& other ) const; 00323 00326 ValueType maxDiffNormImpl( const DenseMatrix<ValueType>& other ) const; 00327 00330 void matrixTimesMatrix( Matrix& result, const Scalar alpha, const Matrix& x,const Scalar beta, const Matrix& y) const; 00331 00332 virtual void prefetch() const; 00333 void prefetch(lama::ContextPtr loc) const; 00334 void wait() const; 00335 00338 const DenseStorage<ValueType>& getLocalStorage() const; 00339 00340 DenseStorage<ValueType>& getLocalStorage(); 00341 00344 virtual IndexType getLocalNumValues() const; 00345 virtual IndexType getLocalNumRows() const; 00346 virtual IndexType getLocalNumColumns() const; 00347 00348 //todo: no instantiation of these functions --> implement or delete 00349 //IndexType getNumLocalChunks( ) const; 00350 //IndexType getNumTotalChunks( ) const; 00351 00352 virtual IndexType getNumValues() const; 00353 00354 std::vector<DenseStoragePtr>& getCyclicLocalValues( ); 00355 00356 const std::vector<DenseStoragePtr>& getCyclicLocalValues( ) const; 00357 00358 virtual bool hasDiagonalProperty() const; 00359 00360 virtual void resetDiagonalProperty(); 00361 00362 virtual void writeAt( std::ostream& stream ) const; 00363 00364 virtual Scalar::ScalarType getValueType() const; 00365 00371 void writeToFile( const std::string& fileName, 00372 const File::FileType fileType = File::BINARY, 00373 const File::DataType dataType = File::INTERNAL, 00374 const File::IndexDataType indexDataTypeIA = File::INT, 00375 const File::IndexDataType indexDataTypeJA = File::INT ) const; 00379 virtual std::auto_ptr<Matrix> create() const; 00380 00384 virtual std::auto_ptr<Matrix> copy() const; 00385 00386 virtual size_t getMemoryUsage() const; 00387 00390 std::vector<boost::shared_ptr<DenseStorage<ValueType> > > mData; 00391 00392 using CRTPMatrix<DenseMatrix<T>, T >::getNumRows; 00393 using CRTPMatrix<DenseMatrix<T>, T >::getNumColumns; 00394 00395 using CRTPMatrix<DenseMatrix<T>, T >::getDistribution; 00396 using CRTPMatrix<DenseMatrix<T>, T >::getDistributionPtr; 00397 using CRTPMatrix<DenseMatrix<T>, T >::getColDistribution; 00398 using CRTPMatrix<DenseMatrix<T>, T >::getColDistributionPtr; 00399 00400 const std::vector<PartitionId>& getOwners() const { return mOwners; } 00401 00402 protected: 00403 00404 using CRTPMatrix<DenseMatrix<T>, T >::mNumRows; 00405 using CRTPMatrix<DenseMatrix<T>, T >::mNumColumns; 00406 00407 std::vector<PartitionId> mOwners; 00408 00409 private: 00410 00413 void allocateData(); 00414 00415 /*************************************************************************** 00416 * Static Methods for dense storage * 00417 ***************************************************************************/ 00418 00428 static void joinColumnData( DenseStorage<ValueType>& result, 00429 const std::vector<boost::shared_ptr< DenseStorage<ValueType> > >& chunks, 00430 const std::vector<IndexType>& columnOwners); 00431 00440 static void splitColumnData( std::vector<boost::shared_ptr< DenseStorage<ValueType> > >& chunks, 00441 const DenseStorage<ValueType>& columnData, 00442 const PartitionId numChunks, 00443 const std::vector<IndexType>& columnOwners ); 00444 00445 00453 static void localize( DenseStorage<ValueType>& local, 00454 const DenseStorage<ValueType>& global, 00455 const Distribution& rowDistribution ); 00456 00459 template<typename otherT> 00460 void copyDenseMatrix( const DenseMatrix<otherT>& other ); 00461 00464 template<typename OtherT> 00465 void getDiagonalImpl( DenseVector<OtherT>& diagonal ) const; 00466 00467 void redistributeRows( DistributionPtr rowDistribution ); 00468 00471 void splitColumns( DistributionPtr colDistribution ); 00472 00473 void getRow( DenseVector<ValueType>& row, const IndexType i ) const; 00474 00475 mutable LAMAArray<ValueType> mSendValues; 00476 mutable LAMAArray<ValueType> mReceiveValues; 00477 00478 //todo: no implementation: implement or delete 00479 //void initChunks(); // common initialization for constructors 00480 00481 LAMA_LOG_DECL_STATIC_LOGGER(logger); 00482 00483 void computeOwners(); 00484 00487 void invertCyclic(); 00488 }; 00489 00490 /* template methods implementations */ 00491 00492 template<typename ValueType> 00493 template<typename OtherValueType> 00494 void DenseMatrix<ValueType>::copyDenseMatrix( const DenseMatrix<OtherValueType>& other ) 00495 { 00496 // check for valid pointer, might be dynamic cast went wrong somewhere else 00497 00498 LAMA_ASSERT_ERROR( &other, "NULL matrix in assignment operator" ); 00499 00500 LAMA_LOG_INFO( logger, "copy dense, this = " << this << ", other = " << &other ); 00501 00502 // inherit size and distributions 00503 00504 Matrix::setDistributedMatrix( other.getDistributionPtr(), other.getColDistributionPtr() ); 00505 00506 mData.resize( other.mData.size() ); 00507 00508 IndexType n = static_cast<IndexType> ( other.mData.size() ); 00509 00510 for( IndexType i = 0; i < n; ++i ) 00511 { 00512 LAMA_LOG_DEBUG( logger, "copy block " << i << " of " << n 00513 << " = " << *other.mData[i] ); 00514 00515 mData[i].reset( new DenseStorage<ValueType>( *other.mData[i] ) ); 00516 } 00517 00518 mOwners = other.getOwners(); 00519 } 00520 00521 template<typename ValueType> 00522 template<typename OtherValueType> 00523 void DenseMatrix<ValueType>::setRawDenseData( const IndexType numRows, 00524 const IndexType numColumns, 00525 const OtherValueType values[], 00526 const OtherValueType eps ) 00527 { 00528 allocate( numRows, numColumns ); 00529 mData[0]->setRawDenseData( numRows, numColumns, values, eps ); 00530 } 00531 00532 template<typename ValueType> 00533 template<typename OtherValueType> 00534 DenseMatrix<ValueType>::DenseMatrix( 00535 const IndexType numRows, 00536 const IndexType numColumns, 00537 const IndexType numNoneZeros, 00538 const IndexType* const ia, 00539 const IndexType* const ja, 00540 const OtherValueType* const values ) 00541 00542 : CRTPMatrix<DenseMatrix<ValueType>, ValueType >( numRows, numColumns ) 00543 { 00544 mData.resize(1); 00545 mData[0].reset( new DenseStorage<ValueType>( mNumRows, mNumColumns) ); 00546 mData[0]->setCSRData(numNoneZeros,ia,ja,values); 00547 computeOwners(); 00548 } 00549 00550 } // namespace lama 00551 00552 #endif // LAMA_DENSEMATRIX_HPP_