LAMA
/home/brandes/workspace/LAMA/src/lama/DenseVector.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_DENSE_VECTOR_HPP_
00034 #define LAMA_DENSE_VECTOR_HPP_
00035 
00036 // for dll_import
00037 #include <lama/config.hpp>
00038 
00039 // base classes
00040 #include <lama/Vector.hpp>
00041 
00042 // others
00043 #include <lama/HostWriteAccess.hpp>
00044 #include <lama/HostReadAccess.hpp>
00045 #include <lama/LAMAArray.hpp>
00046 #include <lama/SyncToken.hpp>
00047 #include <lama/TypeTraits.hpp>
00048 
00049 #include <lama/distribution/Distribution.hpp>
00050 #include <lama/distribution/Halo.hpp>
00051 #include <lama/distribution/HaloBuilder.hpp>
00052 
00053 #include <lama/exception/Exception.hpp>
00054 
00055 #include <lama/io/mmio.hpp>
00056 #include <lama/io/FileType.hpp>
00057 #include <lama/io/XDRFileStream.hpp>
00058 
00059 #include <fstream>
00060 
00061 namespace lama
00062 {
00063 
00069 template<typename T>
00070 class LAMA_DLL_IMPORTEXPORT DenseVector : public Vector
00071 {
00072 public:
00073 
00077     typedef T ValueType;
00078 
00081     DenseVector();
00082 
00088     DenseVector( DistributionPtr distribution);
00089 
00096     DenseVector( const IndexType size, const ValueType value );
00097 
00104     DenseVector( DistributionPtr distribution, const ValueType value );
00105 
00114     template<typename OtherValueType>
00115     DenseVector(const IndexType size,
00116         const OtherValueType* values);
00117 
00125     DenseVector( const DenseVector<ValueType>& other );
00126 
00131     DenseVector( const Vector& other );
00132 
00141     DenseVector( const Vector& other, DistributionPtr distribution );
00142 
00149     DenseVector( const _LAMAArray& localValues, DistributionPtr distribution );
00150 
00162     DenseVector( const std::string& filename );
00163 
00169     DenseVector(const Expression<Scalar,Vector,Times>& expression);
00170 
00176     DenseVector(const Expression<Expression<Scalar,Vector,Times>, Expression<Scalar,Vector,Times>, Plus>& expression);
00177 
00178     /* --------------------------------------------------------------------- */
00179 
00185     DenseVector(const Expression<Expression<Scalar, Expression<Matrix, Vector, Times>, Times>, 
00186                                  Expression<Scalar, Vector,Times>, 
00187                                  Plus>& expression);
00193     DenseVector(const Expression<Scalar,Expression<Matrix, Vector, Times>, Times>& expression);
00194 
00200     DenseVector(const Expression<Matrix, Vector, Times>& expression);
00201 
00205     virtual ~DenseVector();
00206 
00209     void allocate( DistributionPtr distribution );
00210 
00216     DenseVector& operator=( const DenseVector<T>& other );
00217 
00220     DenseVector& operator=( const Scalar );
00221 
00222     virtual Scalar::ScalarType getValueType() const;
00223 
00227     virtual void buildValues( _LAMAArray& values ) const;
00228 
00232     virtual void setValues( const _LAMAArray& values );
00233 
00234     virtual std::auto_ptr<Vector> create() const;
00235 
00236     virtual std::auto_ptr<Vector> create( DistributionPtr distribution ) const;
00237 
00238     //TODO: We either need a none const getLocalValues()
00239     // or an operator[] with local sematics or both
00240     // i guess both is the most intuitive because a user that does not request
00241     // a parallel environment expects the operator[] to exists
00242     // and for users of a distributed DenseVector the usage of
00243     // getLocalValues and getHaloValues is more explicite and there for
00244     // better understandable and less errorprone.
00245     // Maybe an access proxy would be a nice solution, because with a proxy we
00246     // can avoid to change the size and other attributes of the LAMAArray
00247     // mLocalValues.
00254     LAMAArray<T>& getLocalValues() { return mLocalValues; }
00255 
00261     const LAMAArray<T>& getLocalValues() const { return mLocalValues; }
00262 
00268     LAMAArray<T>& getHaloValues() const { return mHaloValues; }
00269 
00275     void updateHalo( const Halo& halo ) const;
00276 
00283     std::auto_ptr<SyncToken> updateHaloAsync( const Halo& halo ) const;
00284 
00285     virtual Scalar getValue(IndexType globalIndex) const;
00286 
00287     virtual Scalar min() const;
00288 
00289     virtual Scalar max() const;
00290 
00291     virtual Scalar l1Norm() const;
00292 
00293     virtual Scalar l2Norm() const;
00294 
00295     virtual Scalar maxNorm() const;
00296 
00297     static void vectorPlusVector( ContextPtr context, LAMAArrayView<T> result,
00298                                     const T alpha, const LAMAArrayConstView<T> x,
00299                                     const T beta, const LAMAArrayConstView<T> y );
00300 
00301     virtual void swap(Vector& other);
00302 
00303     virtual void writeAt(std::ostream& stream) const;
00304 
00305     virtual void assign( const Expression<Expression<Scalar,Vector,Times>,
00306                                           Expression<Scalar,Vector,Times>,
00307                                           Plus >& expression );
00308 
00311     virtual void assign( const Scalar value );
00312 
00315     virtual void assign( const Vector& other );
00316 
00317     virtual void assign( const _LAMAArray& localValues, DistributionPtr dist );
00318 
00319     virtual void buildLocalValues( _LAMAArray& localValues ) const;
00320 
00321     virtual Scalar dotProduct( const Vector& other ) const;
00322 
00323     virtual void prefetch( const ContextPtr location ) const;
00324 
00325     virtual void wait() const;
00326 
00327     virtual void invert();
00328 
00329     virtual size_t getMemoryUsage() const;
00330 
00331     virtual void redistribute( DistributionPtr distribution );
00332 
00333     void writeToFile( const std::string& fileBaseName,
00334                       const File::FileType fileType = File::XDR,
00335                       const File::DataType dataType = File::DOUBLE ) const;
00336 
00337 protected:
00338 
00339     virtual void resizeImpl();
00340 
00341     using Vector::mContext;
00342 
00343     LAMA_LOG_DECL_STATIC_LOGGER(logger);
00344 
00345 private:
00346 
00347     long getDataTypeSize( const File::DataType dataType) const;
00348 
00349     void writeVectorToFormattedFile(const std::string& fileName) const;
00350 
00351     void writeVectorToBinaryFile(
00352         const std::string& fileName,
00353         const long outputDataTypeSize ) const;
00354 
00355     void writeVectorToXDRFile(
00356         const std::string& fileName,
00357         const long outputDataTypeSize ) const;
00358 
00359     void writeVectorDataToBinaryFile(
00360         std::fstream& outFile,
00361         const long dataTypeSize ) const;
00362 
00363     void readVectorHeader( const std::string& filename, File::FileType& fileType, long& dataTypeSize );
00364 
00365     void writeVectorHeader(
00366         const std::string& fileName,
00367         const File::FileType& fileType,
00368         const long dataTypeSize ) const;
00369 
00370     void writeVectorToMMFile(
00371         const std::string& filename,
00372         const File::DataType& dataType ) const;
00373 
00374     void readVectorFromFormattedFile( const std::string& fileName );
00375 
00376     void readVectorFromBinaryFile(
00377             const std::string& fileName,
00378             const long dataTypeSize );
00379 
00380     void readVectorFromXDRFile(
00381             const std::string& fileName,
00382             const long dataTypeSizeHeader );
00383 
00384     void readVectorDataFromBinaryFile(
00385             std::fstream &inFile,
00386             const long dataTypeSize );
00387 
00388     LAMAArray<T> mLocalValues; 
00389 
00390     mutable LAMAArray<T> mHaloValues;  
00391 };
00392 
00393 /* ------------------------------------------------------------------------- */
00394 
00395 template<typename T>
00396 template<typename OtherValueType>
00397 DenseVector<T>::DenseVector(const IndexType size, const OtherValueType* values)
00398     : Vector(size)
00399 {
00400     HostWriteOnlyAccess<T> writeAccess( mLocalValues, size );
00401 
00402 #pragma omp parallel for schedule(LAMA_OMP_SCHEDULE)
00403     for ( IndexType i = 0; i < size; i++ )
00404     {
00405         writeAccess[i] = values[i];
00406     }
00407 
00408     // Halo is not used yet
00409 }
00410 
00411 template<typename T>
00412 DenseVector<T>::DenseVector( const DenseVector<ValueType>& other )
00413 
00414    : Vector( other )
00415 
00416 {
00417     // implementation here can be simpler as DenseVector( const Vector& other ) 
00418 
00419     LAMA_LOG_INFO( logger, "Copy of vector of global size " << size()
00420                       << ", local size " << getDistribution().getLocalSize() );
00421 
00422     mLocalValues = other.getLocalValues();
00423 }
00424 
00425 }
00426 
00427 #endif // LAMA_DENSE_VECTOR_HPP_