PetscVector.hxx
1 // Copyright (C) 2011-2012, INRIA
2 // Author(s): Marc Fragu
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_VECTOR_PETSCVECTOR_HXX
22 
23 #include "../share/Common.hxx"
24 #include "../share/Properties.hxx"
25 #include "../share/Storage.hxx"
26 #include "../share/Errors.hxx"
27 #include "../share/Allocator.hxx"
28 
29 #include "petscvec.h"
30 
31 
32 namespace Seldon
33 {
34 
35 
37 
40  template <class T, class Allocator>
41  class PETScVector: public Vector_Base<T, Allocator>
42  {
43  public:
44  typedef typename Allocator::value_type value_type;
45  typedef typename Allocator::pointer pointer;
46  typedef typename Allocator::const_pointer const_pointer;
47  typedef typename Allocator::reference reference;
48  typedef typename Allocator::const_reference const_reference;
49 
56 
57  // Methods.
58  public:
59  // Constructor.
60  explicit PETScVector();
61  explicit PETScVector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
62  PETScVector(Vec& petsc_vector);
64 
65  // Destructor.
66  ~PETScVector();
67 
68  Vec& GetPetscVector();
69  const Vec& GetPetscVector() const;
70  void SetCommunicator(MPI_Comm mpi_communicator);
71 
72  void Clear();
73 
74  void Resize(int i);
75  void SetData(int i, pointer data);
76  void Nullify();
77 
78  // Element access.
79  value_type operator() (int i) const;
80  void SetBuffer(int i, T value, InsertMode insert_mode = INSERT_VALUES);
81  void Flush();
82  void GetProcessorRange(int& i, int& j) const;
83  void Copy(const PETScVector<T, Allocator>& X);
84  void Copy(const Vec& petsc_vector);
85  void Append(const T& x);
86 
87  // Basic functions.
88  int GetDataSize() const;
89  int GetLocalM() const;
90 
91  // Convenient functions.
92  void Zero();
93  void Fill();
94  template <class T0>
95  void Fill(const T0& x);
96  void FillRand();
97 
98  // Norms.
99  value_type GetNormInf() const;
100  int GetNormInfIndex() const;
101  };
102 
103 
104  template <class T, class Allocator>
105  class Vector<T, PETScSeq, Allocator>: public PETScVector<T, Allocator>
106  {
107  public:
108  typedef typename Allocator::value_type value_type;
109  typedef typename Allocator::pointer pointer;
110  typedef typename Allocator::const_pointer const_pointer;
111  typedef typename Allocator::reference reference;
112  typedef typename Allocator::const_reference const_reference;
113 
114  typedef PETScSeq storage;
115 
116  // Methods.
117  public:
118  // Constructor.
119  explicit Vector();
120  explicit Vector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
121  Vector(Vec& petsc_vector);
123  // Destructor.
124  ~Vector();
125 
126  void Copy(const Vector<T, PETScSeq, Allocator>& X);
127  void Copy(const Vec& petsc_vector);
128  // Memory management.
129  void Reallocate(int i);
130 
131 #ifndef SWIG
132  Vector<T, PETScSeq, Allocator>& operator= (const Vector<T,
133  PETScSeq, Allocator>& X);
134  template <class T0>
135  Vector<T, PETScSeq, Allocator>& operator= (const T0& X);
136 #endif
137  template <class T0>
138  Vector<T, PETScSeq, Allocator>& operator*= (const T0& X);
139 
140  void Print() const;
141 
142  // Input/output functions.
143  void Write(string FileName, bool with_size = true) const;
144  void Write(ostream& FileStream, bool with_size = true) const;
145  void WriteText(string FileName) const;
146  void WriteText(ostream& FileStream) const;
147  void Read(string FileName, bool with_size = true);
148  void Read(istream& FileStream, bool with_size = true);
149  void ReadText(string FileName);
150  void ReadText(istream& FileStream);
151  };
152 
153 
154 #ifndef SWIG
155  template <class T, class Allocator>
156  ostream& operator << (ostream& out,
158 #endif
159 
160 
161  template <class T, class Allocator>
162  class Vector<T, PETScPar, Allocator>: public PETScVector<T, Allocator>
163  {
164  public:
165  typedef typename Allocator::value_type value_type;
166  typedef typename Allocator::pointer pointer;
167  typedef typename Allocator::const_pointer const_pointer;
168  typedef typename Allocator::reference reference;
169  typedef typename Allocator::const_reference const_reference;
170 
171  typedef PETScPar storage;
172 
173  // Methods.
174  public:
175  // Constructor.
176  explicit Vector();
177  explicit Vector(int i, MPI_Comm mpi_communicator = MPI_COMM_WORLD);
178  explicit Vector(int i, int Nlocal, MPI_Comm mpi_communicator);
179  Vector(Vec& petsc_vector);
181  // Destructor.
182  ~Vector();
183 
184  void Copy(const Vector<T, PETScPar, Allocator>& X);
185  void Copy(const Vec& petsc_vector);
186  // Memory management.
187  void Reallocate(int i, int local_size = PETSC_DECIDE);
188 
189 #ifndef SWIG
190  Vector<T, PETScPar, Allocator>& operator= (const Vector<T,
191  PETScPar, Allocator>& X);
192  template <class T0>
193  Vector<T, PETScPar, Allocator>& operator= (const T0& X);
194 #endif
195  template <class T0>
196  Vector<T, PETScPar, Allocator>& operator*= (const T0& X);
197 
198  void Print() const;
199 
200  // Input/output functions.
201  void Write(string FileName, bool with_size = true) const;
202  void Write(ostream& FileStream, bool with_size = true) const;
203  void WriteText(string FileName) const;
204  void WriteText(ostream& FileStream) const;
205  void Read(string FileName, bool with_size = true);
206  void Read(istream& FileStream, bool with_size = true);
207  void ReadText(string FileName);
208  void ReadText(istream& FileStream);
209  };
210 
211 
212 #ifndef SWIG
213  template <class T, class Allocator>
214  ostream& operator << (ostream& out,
216 #endif
217 
218 
219 
220 } // namespace Seldon.
221 
222 #define SELDON_FILE_VECTOR_PETSCVECTOR_HXX
223 #endif
Seldon::PETScVector::GetPetscVector
Vec & GetPetscVector()
Returns a reference on the inner petsc vector.
Definition: PetscVectorInline.cxx:106
Seldon::PETScVector::GetProcessorRange
void GetProcessorRange(int &i, int &j) const
Returns the range of indices owned by this processor.
Definition: PetscVectorInline.cxx:280
Seldon::Vector< T, PETScPar, Allocator >
Definition: PetscVector.hxx:162
Seldon::PETScVector::petsc_vector_
Vec petsc_vector_
Encapsulated PETSc vector.
Definition: PetscVector.hxx:51
Seldon::PETScVector::GetLocalM
int GetLocalM() const
Returns the number of elements stored.
Definition: PetscVectorInline.cxx:336
Seldon::PETScVector::mpi_communicator_
MPI_Comm mpi_communicator_
The MPI communicator to use.
Definition: PetscVector.hxx:53
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::PETScVector::Copy
void Copy(const PETScVector< T, Allocator > &X)
Duplicates a vector.
Definition: PetscVectorInline.cxx:296
Seldon::PETScVector::GetNormInf
value_type GetNormInf() const
Returns the infinite norm.
Definition: PetscVector.cxx:136
Seldon::PETScVector::PETScVector
PETScVector()
Default constructor.
Definition: PetscVectorInline.cxx:41
Seldon::PETScVector::Flush
void Flush()
Assembles the PETSc vector.
Definition: PetscVectorInline.cxx:259
Seldon::PETScVector::Append
void Append(const T &x)
Appends an element to the vector.
Definition: PetscVectorInline.cxx:309
Seldon::Vector_Base
Base structure for all vectors.
Definition: SeldonHeader.hxx:201
Seldon::PETScVector
PETSc vector class.
Definition: PetscVector.hxx:41
Seldon::PETScVector::~PETScVector
~PETScVector()
Destructor.
Definition: PetscVectorInline.cxx:95
Seldon::PETScVector::Zero
void Zero()
Sets all elements to zero.
Definition: PetscVector.cxx:67
Seldon::PETScSeq
Definition: StorageInline.cxx:94
Seldon::PETScVector::operator()
value_type operator()(int i) const
Access operator.
Definition: PetscVectorInline.cxx:216
Seldon::PETScVector::FillRand
void FillRand()
Fills the vector randomly.
Definition: PetscVector.cxx:111
Seldon::PETScVector::SetCommunicator
void SetCommunicator(MPI_Comm mpi_communicator)
Sets the MPI communicator.
Definition: PetscVectorInline.cxx:128
Seldon::PETScPar
Definition: StorageInline.cxx:99
Seldon::PETScVector::Clear
void Clear()
Clears the vector.
Definition: PetscVectorInline.cxx:145
Seldon::PETScVector::GetDataSize
int GetDataSize() const
Returns the number of elements stored.
Definition: PetscVectorInline.cxx:325
Seldon::PETScVector::Resize
void Resize(int i)
Changes the length of the vector, and keeps previous values.
Definition: PetscVectorInline.cxx:162
Seldon::PETScVector::Fill
void Fill()
Fills the vector with 0, 1, 2, ...
Definition: PetscVector.cxx:77
Seldon::Vector< T, PETScSeq, Allocator >
Definition: PetscVector.hxx:105
Seldon::PETScVector::SetData
void SetData(int i, pointer data)
Changes the length of the vector and sets its data array (low level method).
Definition: PetscVectorInline.cxx:184
Seldon::PETScVector::SetBuffer
void SetBuffer(int i, T value, InsertMode insert_mode=INSERT_VALUES)
Inserts or adds values into certain locations of a vector.
Definition: PetscVectorInline.cxx:245
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::PETScVector::GetNormInfIndex
int GetNormInfIndex() const
Returns the index of the highest absolute value.
Definition: PetscVector.cxx:151
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617
Seldon::PETScVector::petsc_vector_deallocated_
bool petsc_vector_deallocated_
Boolean to indicate if the inner PETSc vector is destroyed or not.
Definition: PetscVector.hxx:55
Seldon::PETScVector::Nullify
void Nullify()
Clears the vector without releasing memory.
Definition: PetscVectorInline.cxx:198