Warning: this documentation for the development version is under construction.

/home/vivien/public_html/.src_seldon/matrix/PetscMatrix.hxx

00001 // Copyright (C) 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_MATRIX_PETSCMATRIX_HXX
00022 
00023 
00024 #include "../share/Common.hxx"
00025 #include "../share/Properties.hxx"
00026 #include "../share/Storage.hxx"
00027 #include "../share/Errors.hxx"
00028 #include "../share/Allocator.hxx"
00029 
00030 #include <petscmat.h>
00031 
00032 
00033 namespace Seldon
00034 {
00035 
00036 
00038   template <class T, class Prop, class Storage,
00039             class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00040   class PetscMatrix: public Matrix_Base<T, Allocator>
00041   {
00042     // Typedef declaration.
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     typedef typename Allocator::value_type entry_type;
00050     typedef typename Allocator::value_type access_type;
00051     typedef typename Allocator::value_type const_access_type;
00052 
00053     // Attributes.
00054   protected:
00056     Mat petsc_matrix_;
00058     MPI_Comm mpi_communicator_;
00060     bool petsc_matrix_deallocated_;
00061 
00062     // Methods.
00063   public:
00064     // Constructor.
00065     PetscMatrix();
00066     PetscMatrix(int i, int j);
00067     PetscMatrix(Mat& A);
00068 
00069     void SetCommunicator(MPI_Comm mpi_communicator);
00070     MPI_Comm GetCommunicator() const;
00071 
00072     // Destructor.
00073     ~PetscMatrix();
00074     void Clear();
00075     void Nullify();
00076 
00077     Mat& GetPetscMatrix();
00078     const Mat& GetPetscMatrix() const;
00079 
00080     // Memory management.
00081     void Resize(int i, int j);
00082     void SetData(int i, int j, pointer data);
00083 
00084     const_reference Val(int i, int j) const;
00085     reference Val(int i, int j);
00086     reference operator[] (int i);
00087     const_reference operator[] (int i) const;
00088     void Set(int, int, T);
00089     void SetBuffer(int, int, T, InsertMode);
00090     void Flush() const;
00091     void GetProcessorRowRange(int& i, int& j) const;
00092     void Copy(const Mat& A);
00093 
00094     // Convenient functions.
00095     void Zero();
00096     void SetIdentity();
00097     void Fill();
00098     template <class T0>
00099     void Fill(const T0& x);
00100     void FillRand();
00101 
00102     void Print(int a, int b, int m, int n) const;
00103     void Print(int l) const;
00104 
00105     // Input/output functions.
00106     void Write(string FileName, bool with_size) const;
00107     void Write(ostream& FileStream, bool with_size) const;
00108     void WriteText(string FileName) const;
00109     void WriteText(ostream& FileStream) const;
00110 
00111     void Read(string FileName);
00112     void Read(istream& FileStream);
00113     void ReadText(string FileName);
00114     void ReadText(istream& FileStream);
00115   };
00116 
00117 
00118   template <class T, class Prop, class Allocator>
00119   class Matrix<T, Prop, PETScSeqDense, Allocator>:
00120     public PetscMatrix<T, Prop, RowMajor, Allocator>
00121   {
00122   public:
00123     typedef typename Allocator::value_type value_type;
00124     typedef typename Allocator::pointer pointer;
00125     typedef typename Allocator::const_pointer const_pointer;
00126     typedef typename Allocator::reference reference;
00127     typedef typename Allocator::const_reference const_reference;
00128     typedef typename Allocator::value_type entry_type;
00129     typedef typename Allocator::value_type access_type;
00130     typedef typename Allocator::value_type const_access_type;
00131 
00132   public:
00133     Matrix();
00134     Matrix(int i, int j);
00135     Matrix(Mat& A);
00136     Matrix(const Matrix<T, Prop, PETScSeqDense, Allocator>& A);
00137 
00138     void Reallocate(int i, int j);
00139 
00140     value_type operator() (int i, int j);
00141     value_type operator() (int i, int j) const;
00142 
00143     void Copy(const Matrix<T, Prop, PETScSeqDense, Allocator>& A);
00144     Matrix<T, Prop, PETScSeqDense, Allocator>&
00145     operator= (const Matrix<T, Prop, PETScSeqDense, Allocator>& A);
00146 
00147     void Print() const;
00148   };
00149 
00150 
00151   template <class T, class Prop, class Allocator>
00152   class Matrix<T, Prop, PETScMPIDense, Allocator>:
00153     public PetscMatrix<T, Prop, RowMajor, Allocator>
00154   {
00155   public:
00156     typedef typename Allocator::value_type value_type;
00157     typedef typename Allocator::pointer pointer;
00158     typedef typename Allocator::const_pointer const_pointer;
00159     typedef typename Allocator::reference reference;
00160     typedef typename Allocator::const_reference const_reference;
00161     typedef typename Allocator::value_type entry_type;
00162     typedef typename Allocator::value_type access_type;
00163     typedef typename Allocator::value_type const_access_type;
00164 
00165   public:
00166     Matrix();
00167     Matrix(int i, int j);
00168     Matrix(Mat& A);
00169     Matrix(const Matrix<T, Prop, PETScMPIDense, Allocator>& A);
00170 
00171     void Reallocate(int i, int j, int local_i = PETSC_DECIDE,
00172                     int local_j = PETSC_DECIDE);
00173 
00174     value_type operator() (int i, int j);
00175     value_type operator() (int i, int j) const;
00176 
00177     void Copy(const Matrix<T, Prop, PETScMPIDense, Allocator>& A);
00178     Matrix<T, Prop, PETScMPIDense, Allocator>&
00179     operator= (const Matrix<T, Prop, PETScMPIDense, Allocator>& A);
00180 
00181     void Print() const;
00182   };
00183 
00184 
00185   template <class T, class Prop, class Allocator>
00186   class Matrix<T, Prop, PETScMPIAIJ, Allocator>:
00187     public PetscMatrix<T, Prop, RowMajor, Allocator>
00188   {
00189   public:
00190     typedef typename Allocator::value_type value_type;
00191     typedef typename Allocator::pointer pointer;
00192     typedef typename Allocator::const_pointer const_pointer;
00193     typedef typename Allocator::reference reference;
00194     typedef typename Allocator::const_reference const_reference;
00195     typedef typename Allocator::value_type entry_type;
00196     typedef typename Allocator::value_type access_type;
00197     typedef typename Allocator::value_type const_access_type;
00198 
00199   public:
00200     Matrix();
00201     Matrix(int i, int j);
00202     Matrix(Mat& A);
00203     Matrix(const Matrix<T, Prop, PETScMPIAIJ, Allocator>& A);
00204 
00205     void Reallocate(int i, int j, int local_i = PETSC_DECIDE,
00206                     int local_j = PETSC_DECIDE);
00207 
00208     value_type operator() (int i, int j);
00209     value_type operator() (int i, int j) const;
00210 
00211     void Copy(const Matrix<T, Prop, PETScMPIAIJ, Allocator>& A);
00212     Matrix<T, Prop, PETScMPIAIJ, Allocator>&
00213     operator= (const Matrix<T, Prop, PETScMPIAIJ, Allocator>& A);
00214 
00215     template <class T0,  class Allocator0>
00216     void Copy(const Matrix<T0, General, RowSparse, Allocator0>& A);
00217 
00218     int GetLocalM() const;
00219     int GetLocalN() const;
00220 
00221     void Print() const;
00222   };
00223 
00224 
00225 } // namespace Seldon.
00226 
00227 
00228 #define SELDON_FILE_MATRIX_PETSCMATRIX_HXX
00229 #endif