vector/PetscVector.hxx

00001 // Copyright (C) 2011-2012, INRIA
00002 // Author(s): Marc Fragu
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 #ifndef SELDON_FILE_VECTOR_PETSCVECTOR_HXX
00022 
00023 #include "../share/Common.hxx"
00024 #include "../share/Properties.hxx"
00025 #include "../share/Storage.hxx"
00026 #include "../share/Errors.hxx"
00027 #include "../share/Allocator.hxx"
00028 
00029 #include "petscvec.h"
00030 
00031 
00032 namespace Seldon
00033 {
00034 
00035 
00037 
00040   template <class T, class Allocator>
00041   class PETScVector: public Vector_Base<T, Allocator>
00042   {
00043   public:
00044     typedef typename Allocator::value_type value_type;
00045     typedef typename Allocator::pointer pointer;
00046     typedef typename Allocator::const_pointer const_pointer;
00047     typedef typename Allocator::reference reference;
00048     typedef typename Allocator::const_reference const_reference;
00049 
00051     Vec petsc_vector_;
00053     MPI_Comm mpi_communicator_;
00055     bool petsc_vector_deallocated_;
00056 
00057     // Methods.
00058   public:
00059     // Constructor.
00060     explicit PETScVector();
00061     explicit PETScVector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
00062     PETScVector(Vec& petsc_vector);
00063     PETScVector(const PETScVector<T, Allocator>& A);
00064 
00065     // Destructor.
00066     ~PETScVector();
00067 
00068     Vec& GetPetscVector();
00069     const Vec& GetPetscVector() const;
00070     void SetCommunicator(MPI_Comm mpi_communicator);
00071 
00072     void Clear();
00073 
00074     void Resize(int i);
00075     void SetData(int i, pointer data);
00076     void Nullify();
00077 
00078     // Element access.
00079     value_type operator() (int i) const;
00080     void SetBuffer(int i, T value, InsertMode insert_mode = INSERT_VALUES);
00081     void Flush();
00082     void GetProcessorRange(int& i, int& j) const;
00083     void Copy(const PETScVector<T, Allocator>& X);
00084     void Copy(const Vec& petsc_vector);
00085     void Append(const T& x);
00086 
00087     // Basic functions.
00088     int GetDataSize() const;
00089     int GetLocalM() const;
00090 
00091     // Convenient functions.
00092     void Zero();
00093     void Fill();
00094     template <class T0>
00095     void Fill(const T0& x);
00096     void FillRand();
00097 
00098     // Norms.
00099     value_type GetNormInf() const;
00100     int GetNormInfIndex() const;
00101   };
00102 
00103 
00104   template <class T, class Allocator>
00105   class Vector<T, PETScSeq, Allocator>: public PETScVector<T, Allocator>
00106   {
00107   public:
00108     typedef typename Allocator::value_type value_type;
00109     typedef typename Allocator::pointer pointer;
00110     typedef typename Allocator::const_pointer const_pointer;
00111     typedef typename Allocator::reference reference;
00112     typedef typename Allocator::const_reference const_reference;
00113 
00114     typedef PETScSeq storage;
00115 
00116     // Methods.
00117   public:
00118     // Constructor.
00119     explicit Vector();
00120     explicit Vector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
00121     Vector(Vec& petsc_vector);
00122     Vector(const Vector<T, PETScSeq, Allocator>& A);
00123     // Destructor.
00124     ~Vector();
00125 
00126     void Copy(const Vector<T, PETScSeq, Allocator>& X);
00127     void Copy(const Vec& petsc_vector);
00128     // Memory management.
00129     void Reallocate(int i);
00130 
00131 #ifndef SWIG
00132     Vector<T, PETScSeq, Allocator>& operator= (const Vector<T,
00133                                                PETScSeq, Allocator>& X);
00134     template <class T0>
00135     Vector<T, PETScSeq, Allocator>& operator= (const T0& X);
00136 #endif
00137     template <class T0>
00138     Vector<T, PETScSeq, Allocator>& operator*= (const T0& X);
00139 
00140     void Print() const;
00141 
00142     // Input/output functions.
00143     void Write(string FileName, bool with_size = true) const;
00144     void Write(ostream& FileStream, bool with_size = true) const;
00145     void WriteText(string FileName) const;
00146     void WriteText(ostream& FileStream) const;
00147     void Read(string FileName, bool with_size = true);
00148     void Read(istream& FileStream, bool with_size = true);
00149     void ReadText(string FileName);
00150     void ReadText(istream& FileStream);
00151   };
00152 
00153 
00154 #ifndef SWIG
00155   template <class T, class Allocator>
00156   ostream& operator << (ostream& out,
00157                         const Vector<T, PETScSeq, Allocator>& V);
00158 #endif
00159 
00160 
00161   template <class T, class Allocator>
00162   class Vector<T, PETScPar, Allocator>: public PETScVector<T, Allocator>
00163   {
00164   public:
00165     typedef typename Allocator::value_type value_type;
00166     typedef typename Allocator::pointer pointer;
00167     typedef typename Allocator::const_pointer const_pointer;
00168     typedef typename Allocator::reference reference;
00169     typedef typename Allocator::const_reference const_reference;
00170 
00171     typedef PETScPar storage;
00172 
00173     // Methods.
00174   public:
00175     // Constructor.
00176     explicit Vector();
00177     explicit Vector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
00178     explicit Vector(int i, int Nlocal, MPI_Comm mpi_communicator);
00179     Vector(Vec& petsc_vector);
00180     Vector(const Vector<T, PETScPar, Allocator>& A);
00181     // Destructor.
00182     ~Vector();
00183 
00184     void Copy(const Vector<T, PETScPar, Allocator>& X);
00185     void Copy(const Vec& petsc_vector);
00186     // Memory management.
00187     void Reallocate(int i, int local_size = PETSC_DECIDE);
00188 
00189 #ifndef SWIG
00190     Vector<T, PETScPar, Allocator>& operator= (const Vector<T,
00191                                                PETScPar, Allocator>& X);
00192     template <class T0>
00193     Vector<T, PETScPar, Allocator>& operator= (const T0& X);
00194 #endif
00195     template <class T0>
00196     Vector<T, PETScPar, Allocator>& operator*= (const T0& X);
00197 
00198     void Print() const;
00199 
00200     // Input/output functions.
00201     void Write(string FileName, bool with_size = true) const;
00202     void Write(ostream& FileStream, bool with_size = true) const;
00203     void WriteText(string FileName) const;
00204     void WriteText(ostream& FileStream) const;
00205     void Read(string FileName, bool with_size = true);
00206     void Read(istream& FileStream, bool with_size = true);
00207     void ReadText(string FileName);
00208     void ReadText(istream& FileStream);
00209   };
00210 
00211 
00212 #ifndef SWIG
00213   template <class T, class Allocator>
00214   ostream& operator << (ostream& out,
00215                         const Vector<T, PETScPar, Allocator>& V);
00216 #endif
00217 
00218 
00219 
00220 } // namespace Seldon.
00221 
00222 #define SELDON_FILE_VECTOR_PETSCVECTOR_HXX
00223 #endif