MyraMath
SparseZLDLTSolver.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_MULTIFRONTAL_SPARSEZLDLTSOLVER_H
7 #define MYRAMATH_MULTIFRONTAL_SPARSEZLDLTSOLVER_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 
16 // For ReflectNumber<>
18 
19 // Return type for all _jobgraph() methods.
21 
22 // Options pack.
24 
25 // Underlying numeric storage.
27 #include <myramath/multifrontal/detail/llt/LContainer.h>
28 
29 // Permutation type.
30 #include <vector>
31 
32 namespace myra {
33 
34 // Forward declarations, components.
35 class AssemblyTree;
36 template <class Precision> class ZLDLTKernel;
37 
38 // Forward declarations, A type.
39 class Permutation;
40 template<class Number> class SparseMatrix;
41 template<class Number> class SparseMatrixRange;
42 template<class Number> class CSparseMatrixRange;
43 
44 // Forward declarations, X/B types.
45 class intCRange;
46 template <class Number> class Matrix;
47 template <class Number> class MatrixRange;
48 template <class Number> class CMatrixRange;
49 template <class Number> class Vector;
50 template <class Number> class VectorRange;
51 template <class Number> class CVectorRange;
52 template <class Number> class LowerMatrix;
53 template <class Number> class LowerMatrixRange;
54 template <class Number> class CLowerMatrixRange;
55 
56 // Forward declarations, serialization.
57 class InputStream;
58 class OutputStream;
59 
61 template<class Precision> class MYRAMATH_EXPORT SparseZLDLTSolver
62  {
63  public:
64 
65  // Useful typedefs for various dense/sparse ranges.
66  typedef std::complex<Precision> Number;
67  typedef MatrixRange<Number> DRange; // Dense range type (e.g. right hand sides)
68  typedef LowerMatrixRange<Number> LRange; // Lower range type (e.g. symmetric schur complement)
69  typedef CSparseMatrixRange<Number> SRange; // Sparse range type (e.g. underlying A)
70  typedef intCRange iRange; // Indices range type (e.g. stencils for partial solve etc)
71 
72  // Typedef for Options pack.
73  typedef ::myra::multifrontal::Options Options;
74 
75  // ----------------------------------------- Construction, serialization, value semantics.
76 
79 
82 
84  void swap(SparseZLDLTSolver& that);
85 
86 #ifdef MYRAMATH_ENABLE_CPP11
89 #endif
90 
92  SparseZLDLTSolver& operator = (SparseZLDLTSolver that);
93 
95  explicit SparseZLDLTSolver(InputStream& in);
96 
98  void write(OutputStream& out) const;
99 
102 
103  // ----------------------------------------- Factorization and factorizing constructors.
104 
106  SparseZLDLTSolver(const SRange& in_A, Options options = defaults());
107  void factor(const SRange& A, Options options = defaults());
108  JobGraph factor_jobgraph(const SRange& A, Options options = defaults());
109 
111  SparseZLDLTSolver(const SRange& in_A, const Permutation& P, Options options = defaults());
112  void factor(const SRange& in_A, const Permutation& P, Options options = defaults());
113  JobGraph factor_jobgraph(const SRange& in_A, const Permutation& P, Options options = defaults());
114 
116  SparseZLDLTSolver(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
117  void factor(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
118  JobGraph factor_jobgraph(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
119 
120  // ----------------------------------------- Linear solution.
121 
123  // side = Solve by A from the 'L'eft or from the 'R'ight?
124  // op = Apply an operation to A? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
125  void solve(const DRange& B, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
126  JobGraph solve_jobgraph(const DRange& B, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
127 
129  // side = Solve by L from the 'L'eft or from the 'R'ight?
130  // op = Apply an operation to L? ('T'ranspose or 'N'othing)
131  void solveL(const DRange& B, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
132  JobGraph solveL_jobgraph(const DRange& B, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
133 
135  // side = Solve by D from the 'L'eft or from the 'R'ight?
136  // Since D is filled with +1/-1's only, there's no op modifier (they'd all do the same thing)
137  void solveD(const DRange& B, char side = 'L') const;
138 
139  // ----------------------------------------- Solution with refinement.
140 
142  // side = Solve by A from the 'L'eft or from the 'R'ight?
143  // op = Apply an operation to A? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
144  // Note, this is more expensive because it calls solve() multiple times, and sparse symm() too.
145  // Returns the residual history, frobenius(A*X-B), evaluated at each refinement iteration.
146  std::vector<Precision> refine(const DRange& B, char side = 'L', char op = 'N', Precision tolerance = default_tolerance(), int iterations = default_iterations(), Options options = defaults().set_nthreads(1)) const;
148  friend class SparseZLDLTSolver_RefineAction;
149 
150  // ----------------------------------------- Calculating schur complements.
151 
153  LowerMatrix<Number> schur(const SRange& B, Options options = defaults()) const;
154  void schur_inplace(const SRange& B, const LRange& S, Options options = defaults()) const;
155  JobGraph schur_jobgraph(const SRange& B, const LRange& S, Options options = defaults()) const;
156 
158  LowerMatrix<Number> schur(const iRange& Bi, const DRange& Bv, Options options = defaults()) const;
159  void schur_inplace(const iRange& Bi, const DRange& Bv, const LRange& S, Options options = defaults()) const;
160  JobGraph schur_jobgraph(const iRange& Bi, const DRange& Bv, const LRange& S, Options options = defaults()) const;
161 
163  Matrix<Number> schur(const SRange& B, const SRange& C, Options options = defaults()) const;
164  void schur_inplace(const SRange& B, const SRange& C, const DRange& S, Options options = defaults()) const;
165  JobGraph schur_jobgraph(const SRange& B, const SRange& C, const DRange& S, Options options = defaults()) const;
166 
168  Matrix<Number> schur(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, Options options = defaults()) const;
169  void schur_inplace(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, const DRange& S, Options options = defaults()) const;
170  JobGraph schur_jobgraph(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, const DRange& S, Options options = defaults()) const;
171 
172  // ----------------------------------------- Sampling inv(A).
173 
175  Number inverse(int ij) const;
176 
178  Number inverse(int i, int j) const;
179 
181  Matrix<Number> inverse(const iRange& i, const iRange& j, Options options = defaults()) const;
182  void inverse_inplace(const iRange& i, const iRange& j, const DRange& Z, Options options = defaults()) const;
183  JobGraph inverse_jobgraph(const iRange& i, const iRange& j, const DRange& Z, Options options = defaults()) const;
184 
186  LowerMatrix<Number> inverse(const iRange& ij, Options options = defaults()) const;
187  void inverse_inplace(const iRange& ij, const LRange& Z, Options options = defaults()) const;
188  JobGraph inverse_jobgraph(const iRange& ij, const LRange& Z, Options options = defaults()) const;
189 
190  // ----------------------------------------- Partial linear solution.
191 
193  // Note X = partialsolve(linspace(0,N),linspace(0,N),B,side,op) yields the same result X = solve(B,side,op)
194  // Note X = partialsolve(i,j,B,'L'eft, op) yields the same result as X = op(this->inverse(i,j))*B
195  // Note X = partialsolve(i,j,B,'R'ight,op) yields the same result as X = B*op(this->inverse(i,j))
196  Matrix<Number> partialsolve(const iRange& i, const iRange& j, const CMatrixRange<Number>& B, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
197  void partialsolve_inplace(const iRange& i, const iRange& j, const CMatrixRange<Number>& B, const MatrixRange<Number>& X, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
198  JobGraph partialsolve_jobgraph(const iRange& i, const iRange& j, const CMatrixRange<Number>& B, const MatrixRange<Number>& X, char side = 'L', char op = 'N', Options options = defaults().set_nthreads(1)) const;
199 
200  // ----------------------------------------- Miscellany.
201 
203  int size() const;
204 
206  std::pair<int,int> inertia() const;
207 
209  const AssemblyTree& tree() const;
210 
212  static Options defaults();
213  static Precision default_tolerance();
214  static int default_iterations();
215 
216  private:
217 
218  // Numeric contents.
220  typedef ::myra::multifrontal::detail::llt::LContainer<Kernel> LContainer;
221  LContainer L;
222 
223  };
224 
226 template<class Precision> class ReflectNumber <SparseZLDLTSolver<Precision> >
227  { public: typedef std::complex<Precision> type; };
228 
230 MYRAMATH_EXPORT Vector<NumberC> operator * (const SparseZLDLTSolver<float >& solver, const CVectorRange<NumberC>& x);
232 MYRAMATH_EXPORT Vector<NumberZ> operator * (const SparseZLDLTSolver<double>& solver, const CVectorRange<NumberZ>& x);
234 
236 MYRAMATH_EXPORT Matrix<NumberC> operator * (const SparseZLDLTSolver<float >& solver, const CMatrixRange<NumberC>& X);
238 MYRAMATH_EXPORT Matrix<NumberZ> operator * (const SparseZLDLTSolver<double>& solver, const CMatrixRange<NumberZ>& X);
240 
241 } // namespace myra
242 
243 #endif
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
Sparse direct solver suitable for complex symmetric systems.
Definition: SparseZLDLTSolver.h:61
Options pack for routines in /multifrontal.
Definition: Options.h:24
Represents a Permutation matrix, used to reorder rows/columns/etc of various numeric containers...
Definition: Permutation.h:34
Symbolic analysis data structure for all multifrontal solvers.
Definition: AssemblyTree.h:38
Represents a mutable LowerMatrixRange.
Definition: conjugate.h:28
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Definition: SparseZLDLTSolver.cpp:334
Type erasure class that wraps JobGraphBase, gives it value semantics.
Definition: JobGraph.h:64
Definition: syntax.dox:1
Represents a const MatrixRange.
Definition: bothcat.h:22
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Abstraction for representing a directed acyclic graph of Job&#39;s.
Various utility functions/classes related to scalar Number types.
Represents a mutable MatrixRange.
Definition: conjugate.h:26
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
Represents a const SparseMatrixRange.
Definition: bothcat.h:24
Options pack for routines in /multifrontal.
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
Factors A into L*L&#39;, presents solve methods.
Definition: Kernel.h:40
Represents a const VectorRange.
Definition: axpy.h:20
Stores a lower triangular matrix in rectangular packed format.
Definition: conjugate.h:22
Pivot factorization for SparseZLDLTSolver.
Represents a const intRange.
Definition: intRange.h:142