LAMA
|
00001 00033 #ifndef LAMA_DIASTORAGE_HPP_ 00034 #define LAMA_DIASTORAGE_HPP_ 00035 00036 // for dll_import 00037 #include <lama/config.hpp> 00038 00039 // base classes 00040 #include <lama/storage/CRTPMatrixStorage.hpp> 00041 00042 namespace lama 00043 { 00044 00056 template<typename T> 00057 class LAMA_DLL_IMPORTEXPORT DIAStorage : public CRTPMatrixStorage<DIAStorage<T>, T> 00058 { 00059 public: 00060 00061 typedef T ValueType; 00062 00065 static const char* typeName(); 00066 00073 DIAStorage( const IndexType numRows, const IndexType numColumns ); 00074 00077 DIAStorage(); 00078 00081 DIAStorage( const IndexType numRows, 00082 const IndexType numColumns, 00083 const IndexType numDiagonals, 00084 const LAMAArray<IndexType>& offsets, 00085 const LAMAArray<ValueType>& values ); 00086 00089 DIAStorage( const DIAStorage<ValueType>& other ); 00090 00093 explicit DIAStorage( const _MatrixStorage& other ) { assign(other); } 00094 00097 DIAStorage( const _MatrixStorage& other, const ContextPtr context ) 00098 { setContext(context); assign( other ); } 00099 00102 DIAStorage<ValueType>& operator=( const DIAStorage<ValueType>& other) 00103 { assign( other ); return *this; } 00104 00105 DIAStorage<ValueType>& operator=( const _MatrixStorage& other) 00106 { assign( other ); return *this; } 00107 00110 virtual DIAStorage* copy() const; 00111 00114 virtual DIAStorage* create() const; 00115 00118 virtual void clear(); 00119 00122 virtual ~DIAStorage(); 00123 00128 void check( const char* msg ) const; 00129 00132 virtual MatrixStorageFormat getFormat() const; 00133 00142 void allocate( const IndexType numRows, const IndexType numColumns ); 00143 00146 virtual void purge(); 00147 00148 // /** Switches on/off the diagonal property of the DIA matrix. If on 00149 // * the first column index for each row is the row (diagonal element). 00150 // * 00151 // * If matrix data is already available, the main diagonal elements will 00152 // * be shifted correspondingly. 00153 // */ 00154 // void setDiagonalProperty( bool flag ); 00155 00158 virtual void setIdentity( const IndexType size ); 00159 00171 template<typename OtherValueType> 00172 void getSparseData( 00173 IndexType* ia, 00174 IndexType* ja, 00175 OtherValueType* values, 00176 const IndexType* permutation = NULL ) const; 00177 00186 template<typename OtherValueType> 00187 void buildCSR( LAMAArray<IndexType>& ia, 00188 LAMAArray<IndexType>* ja, 00189 LAMAArray<OtherValueType>* values, 00190 const ContextPtr /* loc */ ) const; 00191 00203 template<typename OtherValueType> 00204 void setCSRDataImpl( const IndexType numRows, 00205 const IndexType numColumns, 00206 const IndexType numValues, 00207 const LAMAArray<IndexType>& ia, 00208 const LAMAArray<IndexType>& ja, 00209 const LAMAArray<OtherValueType>& values, 00210 const ContextPtr loc ); 00211 00214 virtual void matrixTimesVector( 00215 LAMAArrayView<ValueType> result, 00216 const ValueType alpha, const LAMAArrayConstView<ValueType> x, 00217 const ValueType beta, const LAMAArrayConstView<ValueType> y ) const; 00218 00221 virtual void jacobiIterate( 00222 LAMAArrayView<ValueType> solution, const LAMAArrayConstView<ValueType> oldSolution, 00223 const LAMAArrayConstView<ValueType> rhs, const ValueType omega) const; 00224 00227 virtual void writeAt( std::ostream& stream ) const; 00228 00231 const LAMAArray<IndexType>& getOffsets() const; 00232 00233 const LAMAArray<ValueType>& getValues() const; 00234 00237 IndexType getNumDiagonals() const; 00238 00241 template<typename OtherType> 00242 void getRowImpl( LAMAArray<OtherType>& row, const IndexType i ) const; 00243 00250 template<typename OtherType> 00251 void getDiagonalImpl( LAMAArray<OtherType>& diagonal ) const; 00252 00259 template<typename OtherType> 00260 void setDiagonalImpl( const LAMAArray<OtherType>& diagonal ); 00261 00268 void setDiagonalImpl( const Scalar scalar ); 00269 00270 /****************************************************************** 00271 * Scaling of elements in a matrix * 00272 ******************************************************************/ 00273 00276 template<typename OtherType> 00277 void scaleImpl( const LAMAArray<OtherType>& values ); 00278 00281 virtual void scaleImpl( const Scalar value ); 00282 00285 ValueType getValue( IndexType i, IndexType j ) const; 00286 00289 void prefetch( const ContextPtr location ) const; 00290 00293 void wait() const; 00294 00298 void swap( DIAStorage<ValueType>& other ); 00299 00300 virtual size_t getMemoryUsageImpl() const; 00301 00302 using MatrixStorage<ValueType>::mNumRows; 00303 using MatrixStorage<ValueType>::mNumColumns; 00304 using MatrixStorage<ValueType>::mDiagonalProperty; 00305 using MatrixStorage<ValueType>::mRowIndexes; 00306 using MatrixStorage<ValueType>::mCompressThreshold; 00307 00308 using MatrixStorage<ValueType>::prefetch; 00309 using MatrixStorage<ValueType>::getContextPtr; 00310 using MatrixStorage<ValueType>::assign; 00311 using MatrixStorage<ValueType>::setContext; 00312 00313 IndexType mNumDiagonals; 00314 00315 LAMAArray<IndexType> mOffset; 00316 LAMAArray<ValueType> mValues; 00317 00320 void print() const; 00321 00322 private: 00323 00324 virtual bool checkDiagonalProperty() const; 00325 00326 // values might be stored row-wise or diagonal-wise 00327 00328 // take this one for row-major order (fits best for cache architecture) 00329 00330 // inline IndexType index(IndexType irow, IndexType idiag) const { return irow * mNumDiagonals + idiag; } 00331 00332 // take this one for diag-major order (fits best for GPUs) 00333 00334 inline IndexType index(IndexType irow, IndexType idiag) const { return idiag * mNumRows + irow; } 00335 00336 // Help routine to set offset from used diagonals 00337 00338 void setOffsets( const IndexType maxNumDiagonals, const bool upperDiagonalUsed[], const bool lowerDiagonalUsed[] ); 00339 00340 static void setUsedDiagonal(bool upperDiagonalUsed[], bool lowerDiagonalUsed[],IndexType i, IndexType j); 00341 00342 LAMA_LOG_DECL_STATIC_LOGGER( logger ); 00343 }; 00344 00345 } // namespace lama 00346 00347 #endif // LAMA_DIASTORAGE_HPP_