LAMA
/home/brandes/workspace/LAMA/src/lama/matrix/DIASparseMatrix.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_DIA_SPARSE_MATRIX_HPP_
00034 #define LAMA_DIA_SPARSE_MATRIX_HPP_
00035 
00036 // for dll_import
00037 #include <lama/config.hpp>
00038 
00039 // base classes
00040 #include <lama/matrix/SparseMatrix.hpp>
00041 
00042 // others
00043 #include <lama/storage/DIAStorage.hpp>
00044 
00045 #include <lama/distribution/GeneralDistribution.hpp>
00046 
00047 namespace lama
00048 {
00049 
00058 template<typename T>
00059 class LAMA_DLL_IMPORTEXPORT DIASparseMatrix: public SparseMatrix<T>
00060 {
00061 
00062 public:
00063 
00066     typedef T ValueType; 
00067 
00068     typedef DIAStorage<T> StorageType;
00069 
00072     static const char* typeName();
00073 
00076     DIASparseMatrix() 
00077 
00078         : SparseMatrix<ValueType>( createStorage() )
00079     {
00080     }
00081 
00084     DIASparseMatrix( const IndexType numRows, const IndexType numColumns ) 
00085 
00086         : SparseMatrix<ValueType>( createStorage( numRows, numColumns ) )
00087     {
00088     }
00089 
00092     DIASparseMatrix( DistributionPtr rowDist, DistributionPtr colDist ) 
00093 
00094         : SparseMatrix<ValueType>( createStorage( rowDist->getLocalSize(), colDist->getGlobalSize() ),
00095                                    rowDist, colDist  )
00096     {
00097         // Note: splitting of local rows to local + halo part is done by SparseMatrix constructor
00098     }
00099 
00100     DIASparseMatrix( const Matrix& other, bool transposeFlag = false )
00101 
00102         : SparseMatrix<ValueType>( createStorage() )
00103 
00104     { 
00105         if( transposeFlag )
00106         {
00107             SparseMatrix<ValueType>::assignTranspose( other );
00108         }
00109         else
00110         {
00111             SparseMatrix<ValueType>::assign( other );
00112         }
00113     }
00114 
00115     DIASparseMatrix( const Matrix& other, DistributionPtr rowDist, DistributionPtr colDist )
00116 
00117         : SparseMatrix<ValueType>( createStorage() )
00118 
00119     { 
00120         // ToDo: this could be done better to avoid intermediate copies
00121 
00122         SparseMatrix<ValueType>::assign( other ); 
00123         this->redistribute( rowDist, colDist ); 
00124     }
00125 
00136     DIASparseMatrix( const _MatrixStorage& localData, DistributionPtr rowDist, DistributionPtr colDist )
00137 
00138         : SparseMatrix<ValueType>( createStorage() )
00139 
00140     {
00141         SparseMatrix<ValueType>::assign( localData, rowDist, colDist );
00142     }
00143 
00152     DIASparseMatrix( const std::string& filename )
00153 
00154         : SparseMatrix<ValueType>( createStorage() )
00155 
00156     { 
00157         this->readFromFile( filename );
00158     }
00159 
00160     // Expression constructors
00161 
00162     DIASparseMatrix( const Expression<Matrix, Matrix, Times>& expression )
00163 
00164         : SparseMatrix<ValueType>( createStorage() )
00165     {                                                   
00166         SparseMatrix<ValueType>::operator=( expression );
00167     }                                                  
00168 
00169     DIASparseMatrix( const Expression<Scalar, Matrix, Times>& expression )
00170 
00171         : SparseMatrix<ValueType>( createStorage() )
00172     {                                              
00173         SparseMatrix<ValueType>::operator=( expression ); 
00174     }                                                    
00175 
00176     DIASparseMatrix( const Expression<Scalar, Expression<Matrix, Matrix, Times>, Times>& expression )
00177 
00178         : SparseMatrix<ValueType>( createStorage() )
00179     {                                                     
00180         SparseMatrix<ValueType>::operator=( expression ); 
00181     }
00182 
00198     template<typename LocalValueType, typename HaloValueType>
00199     DIASparseMatrix( const IndexType numLocalRows,
00200                      const IndexType numLocalNonZeros,
00201                      const IndexType numHaloNonZeros,
00202                      const IndexType localIA[],
00203                      const IndexType localJA[],
00204                      const LocalValueType localValues[],
00205                      const IndexType haloIA[],
00206                      const IndexType haloJA[],
00207                      const HaloValueType haloValues[],
00208                      const std::vector<IndexType>& ownedIndexes,
00209                      const CommunicatorPtr communicator )
00210 
00211         : SparseMatrix<ValueType>( createStorage() )
00212 
00213 {
00214     LAMA_LOG_INFO( logger, communicator << ": construct distributed matrix "
00215                     << numLocalRows << " by local and halo data + owned indexes" );
00216 
00217     // For the distribution we need the global number of rows, not available as arg, so compute it
00218 
00219     IndexType numGlobalRows = communicator->sum( numLocalRows );
00220 
00221     mLocalData->setRawCSRData( numLocalRows, numLocalRows, numLocalNonZeros, localIA, localJA, localValues );
00222     mHaloData->setRawCSRData( numLocalRows, numGlobalRows, numHaloNonZeros, haloIA, haloJA, haloValues );
00223 
00224     DistributionPtr dist( new GeneralDistribution( numGlobalRows, ownedIndexes, communicator ) );
00225 
00226     // Halo is already splitted, but still contains the global indexes
00227 
00228     mHaloData->buildHalo( mHalo, *dist );  // build halo, maps global indexes to halo indexes
00229 
00230     Matrix::setDistributedMatrix( dist, dist );
00231 }
00232 
00235     virtual const StorageType& getLocalStorage() const;
00236 
00239     virtual StorageType& getLocalStorage();
00240 
00243     virtual const StorageType& getHaloStorage() const;
00244 
00247     virtual void swapLocalStorage( StorageType& localStorage );
00248 
00249     // ToDo: create, copy with covariant return types
00250 
00253     virtual const char* getTypeName() const;
00254 
00255 protected:
00256 
00257     using SparseMatrix<ValueType>::mLocalData;
00258     using SparseMatrix<ValueType>::mHaloData;
00259     using SparseMatrix<ValueType>::mHalo;
00260 
00261 private:
00262 
00265     boost::shared_ptr<MatrixStorage< ValueType> > createStorage() 
00266     {
00267         return boost::shared_ptr<MatrixStorage<ValueType> >( new StorageType() );
00268     }
00269 
00270     boost::shared_ptr<MatrixStorage< ValueType> > createStorage( const IndexType numRows, const IndexType numColumns ) 
00271     {
00272         boost::shared_ptr<MatrixStorage<ValueType> > storage( new StorageType() );
00273         storage->allocate( numRows, numColumns );
00274         return storage;
00275     }
00276 
00277     LAMA_LOG_DECL_STATIC_LOGGER( logger );
00278 };
00279 
00280 
00281 } // namespace lama
00282 
00283 #endif // LAMA_DIA_SPARSE_MATRIX_HPP_