PetscMatrix.hxx
1 // Copyright (C) 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_MATRIX_PETSCMATRIX_HXX
22 
23 
24 #include "../share/Common.hxx"
25 #include "../share/Properties.hxx"
26 #include "../share/Storage.hxx"
27 #include "../share/Errors.hxx"
28 #include "../share/Allocator.hxx"
29 
30 #include <petscmat.h>
31 
32 
33 namespace Seldon
34 {
35 
36 
38  template <class T, class Prop, class Storage, class Allocator
39  = typename SeldonDefaultAllocator<VectFull, T>::allocator>
40  class PetscMatrix: public Matrix_Base<T, Allocator>
41  {
42  // Typedef declaration.
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  typedef typename Allocator::value_type entry_type;
50  typedef typename Allocator::value_type access_type;
51  typedef typename Allocator::value_type const_access_type;
52 
53  // Attributes.
54  protected:
61 
62  // Methods.
63  public:
64  // Constructor.
65  PetscMatrix();
66  explicit PetscMatrix(int i, int j);
67  PetscMatrix(Mat& A);
68 
69  void SetCommunicator(MPI_Comm mpi_communicator);
70  MPI_Comm GetCommunicator() const;
71 
72  // Destructor.
73  ~PetscMatrix();
74  void Clear();
75  void Nullify();
76 
77  Mat& GetPetscMatrix();
78  const Mat& GetPetscMatrix() const;
79 
80  size_t GetMemorySize() const;
81 
82  // Memory management.
83  void Resize(int i, int j);
84  void SetData(int i, int j, pointer data);
85 
86  const_reference Val(int i, int j) const;
87  reference Val(int i, int j);
88  reference operator[] (int i);
89  const_reference operator[] (int i) const;
90  void Set(int, int, T);
91  void SetBuffer(int, int, T, InsertMode);
92  void Flush() const;
93  void GetProcessorRowRange(int& i, int& j) const;
94  void Copy(const Mat& A);
95 
96  // Convenient functions.
97  void Zero();
98  void SetIdentity();
99  void Fill();
100  template <class T0>
101  void Fill(const T0& x);
102  void FillRand();
103 
104  void Print(int a, int b, int m, int n) const;
105  void Print(int l) const;
106 
107  // Input/output functions.
108  void Write(string FileName, bool with_size) const;
109  void Write(ostream& FileStream, bool with_size) const;
110  void WriteText(string FileName) const;
111  void WriteText(ostream& FileStream) const;
112 
113  void Read(string FileName);
114  void Read(istream& FileStream);
115  void ReadText(string FileName);
116  void ReadText(istream& FileStream);
117  };
118 
119 
120  template <class T, class Prop, class Allocator>
121  class Matrix<T, Prop, PETScSeqDense, Allocator>:
122  public PetscMatrix<T, Prop, RowMajor, Allocator>
123  {
124  public:
125  typedef typename Allocator::value_type value_type;
126  typedef typename Allocator::pointer pointer;
127  typedef typename Allocator::const_pointer const_pointer;
128  typedef typename Allocator::reference reference;
129  typedef typename Allocator::const_reference const_reference;
130  typedef typename Allocator::value_type entry_type;
131  typedef typename Allocator::value_type access_type;
132  typedef typename Allocator::value_type const_access_type;
133 
134  public:
135  Matrix();
136  Matrix(int i, int j);
137  Matrix(Mat& A);
139 
140  void Reallocate(int i, int j);
141 
142  value_type operator() (int i, int j);
143  value_type operator() (int i, int j) const;
144 
145  void Copy(const Matrix<T, Prop, PETScSeqDense, Allocator>& A);
147  operator= (const Matrix<T, Prop, PETScSeqDense, Allocator>& A);
148 
149  void Print() const;
150  };
151 
152 
153  template <class T, class Prop, class Allocator>
154  class Matrix<T, Prop, PETScMPIDense, Allocator>:
155  public PetscMatrix<T, Prop, RowMajor, Allocator>
156  {
157  public:
158  typedef typename Allocator::value_type value_type;
159  typedef typename Allocator::pointer pointer;
160  typedef typename Allocator::const_pointer const_pointer;
161  typedef typename Allocator::reference reference;
162  typedef typename Allocator::const_reference const_reference;
163  typedef typename Allocator::value_type entry_type;
164  typedef typename Allocator::value_type access_type;
165  typedef typename Allocator::value_type const_access_type;
166 
167  public:
168  Matrix();
169  Matrix(int i, int j);
170  Matrix(Mat& A);
172 
173  void Reallocate(int i, int j, int local_i = PETSC_DECIDE,
174  int local_j = PETSC_DECIDE);
175 
176  value_type operator() (int i, int j);
177  value_type operator() (int i, int j) const;
178 
179  void Copy(const Matrix<T, Prop, PETScMPIDense, Allocator>& A);
181  operator= (const Matrix<T, Prop, PETScMPIDense, Allocator>& A);
182 
183  void Print() const;
184  };
185 
186 
187  template <class T, class Prop, class Allocator>
188  class Matrix<T, Prop, PETScMPIAIJ, Allocator>:
189  public PetscMatrix<T, Prop, RowMajor, Allocator>
190  {
191  public:
192  typedef typename Allocator::value_type value_type;
193  typedef typename Allocator::pointer pointer;
194  typedef typename Allocator::const_pointer const_pointer;
195  typedef typename Allocator::reference reference;
196  typedef typename Allocator::const_reference const_reference;
197  typedef typename Allocator::value_type entry_type;
198  typedef typename Allocator::value_type access_type;
199  typedef typename Allocator::value_type const_access_type;
200 
201  public:
202  Matrix();
203  Matrix(int i, int j);
204  Matrix(Mat& A);
206 
207  void Reallocate(int i, int j, int local_i = PETSC_DECIDE,
208  int local_j = PETSC_DECIDE);
209 
210  value_type operator() (int i, int j);
211  value_type operator() (int i, int j) const;
212 
213  void Copy(const Matrix<T, Prop, PETScMPIAIJ, Allocator>& A);
215  operator= (const Matrix<T, Prop, PETScMPIAIJ, Allocator>& A);
216 
217  template <class T0, class Allocator0>
218  void Copy(const Matrix<T0, General, RowSparse, Allocator0>& A);
219 
220  int GetLocalM() const;
221  int GetLocalN() const;
222 
223  void Print() const;
224  };
225 
226 
227 } // namespace Seldon.
228 
229 
230 #define SELDON_FILE_MATRIX_PETSCMATRIX_HXX
231 #endif
Seldon::PetscMatrix::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: PetscMatrix.cxx:206
Seldon::PetscMatrix::Zero
void Zero()
Sets all elements to zero.
Definition: PetscMatrix.cxx:55
Seldon::PetscMatrix::Clear
void Clear()
Clears the matrix.
Definition: PetscMatrixInline.cxx:109
Seldon::PetscMatrix::GetMemorySize
size_t GetMemorySize() const
Returns size of A in bytes used to store the matrix.
Definition: PetscMatrixInline.cxx:162
Seldon::PetscMatrix::Write
void Write(string FileName, bool with_size) const
Writes the matrix in a file.
Definition: PetscMatrix.cxx:161
Seldon::Matrix< T, Prop, PETScMPIDense, Allocator >
Definition: PetscMatrix.hxx:154
Seldon::PETScSeqDense
Definition: StorageInline.cxx:104
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::PetscMatrix::Print
void Print(int a, int b, int m, int n) const
Displays a sub-matrix on the standard output.
Definition: PetscMatrix.cxx:122
Seldon::PetscMatrix::GetProcessorRowRange
void GetProcessorRowRange(int &i, int &j) const
Returns the range of row indices owned by this processor.
Definition: PetscMatrixInline.cxx:319
Seldon::PetscMatrix::SetBuffer
void SetBuffer(int, int, T, InsertMode)
Inserts or adds values into certain locations of a matrix.
Definition: PetscMatrixInline.cxx:287
Seldon::PetscMatrix::Val
const_reference Val(int i, int j) const
Access operator.
Definition: PetscMatrixInline.cxx:219
Seldon::PetscMatrix::GetCommunicator
MPI_Comm GetCommunicator() const
Returns the MPI communicator of the current PETSc matrix.
Definition: PetscMatrixInline.cxx:87
Seldon::PetscMatrix::~PetscMatrix
~PetscMatrix()
Destructor.
Definition: PetscMatrixInline.cxx:96
Seldon::Matrix< T, Prop, PETScSeqDense, Allocator >
Definition: PetscMatrix.hxx:121
Seldon::PetscMatrix::SetCommunicator
void SetCommunicator(MPI_Comm mpi_communicator)
Sets the MPI communicator.
Definition: PetscMatrixInline.cxx:75
Seldon::PetscMatrix::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: PetscMatrix.cxx:63
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::PetscMatrix::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: PetscMatrixInline.cxx:128
Seldon::PETScMPIAIJ
Definition: StorageInline.cxx:114
Seldon::PetscMatrix::GetPetscMatrix
Mat & GetPetscMatrix()
Returns a reference on the inner petsc matrix.
Definition: PetscMatrixInline.cxx:141
Seldon::PetscMatrix::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: PetscMatrix.cxx:293
Seldon::PetscMatrix::Resize
void Resize(int i, int j)
Reallocates memory to resize the matrix and keeps previous entries.
Definition: PetscMatrixInline.cxx:178
Seldon::PetscMatrix::operator[]
reference operator[](int i)
Access to elements of the data array.
Definition: PetscMatrixInline.cxx:248
Seldon::PetscMatrix
Matrix class based on PETSc matrix.
Definition: PetscMatrix.hxx:40
Seldon::PetscMatrix::petsc_matrix_
Mat petsc_matrix_
Encapsulated PETSc matrix.
Definition: PetscMatrix.hxx:56
Seldon::Matrix< T, Prop, PETScMPIAIJ, Allocator >
Definition: PetscMatrix.hxx:188
Seldon::PetscMatrix::petsc_matrix_deallocated_
bool petsc_matrix_deallocated_
Boolean to indicate if the inner PETSc matrix is destroyed or not.
Definition: PetscMatrix.hxx:60
Seldon::PetscMatrix::mpi_communicator_
MPI_Comm mpi_communicator_
The MPI communicator to use.
Definition: PetscMatrix.hxx:58
Seldon::PetscMatrix::FillRand
void FillRand()
Fills a matrix randomly.
Definition: PetscMatrix.cxx:101
Seldon::PetscMatrix::SetData
void SetData(int i, int j, pointer data)
Changes the size of the matrix and sets its data array (low level method).
Definition: PetscMatrixInline.cxx:203
Seldon::PETScMPIDense
Definition: StorageInline.cxx:109
Seldon::PetscMatrix::PetscMatrix
PetscMatrix()
Default constructor.
Definition: PetscMatrixInline.cxx:35
Seldon::PetscMatrix::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: PetscMatrix.cxx:252
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::PetscMatrix::Copy
void Copy(const Mat &A)
Duplicates a matrix.
Definition: PetscMatrix.cxx:37
Seldon::PetscMatrix::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: PetscMatrix.cxx:76
Seldon::PetscMatrix::Flush
void Flush() const
Assembles the PETSc matrix.
Definition: PetscMatrixInline.cxx:298