MyraMath
SparseLUSolver.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_SPARSELUSOLVER_H
7 #define MYRAMATH_MULTIFRONTAL_SPARSELUSOLVER_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/lu/LUContainer.h>
28 
29 // Permutation type.
30 #include <vector>
31 
32 namespace myra {
33 
34 // Forward declarations, components.
35 class AssemblyTree;
36 template<class Number> class LUKernel;
37 
38 // Forward declarations, A type.
39 class Permutation;
40 template<class Number> class SparseMatrix;
41 template<class Number> class CSparseMatrixRange;
42 
43 // Forward declarations, X/B types.
44 class intCRange;
45 template<class Number> class Matrix;
46 template<class Number> class MatrixRange;
47 template<class Number> class CMatrixRange;
48 template<class Number> class Vector;
49 template<class Number> class VectorRange;
50 template<class Number> class CVectorRange;
51 
52 // Forward declarations, serialization.
53 class InputStream;
54 class OutputStream;
55 
57 template<class Number> class MYRAMATH_EXPORT SparseLUSolver
58  {
59  public:
60 
61  // Useful typedefs for various dense/sparse ranges.
62  typedef typename ReflectPrecision<Number>::type Precision;
63  typedef MatrixRange<Number> DRange; // Dense range type (e.g. right hand sides)
64  typedef CSparseMatrixRange<Number> SRange; // Sparse range type (e.g. underlying A)
65  typedef intCRange iRange; // Indices range type (e.g. stencils for partial solve etc)
66 
67  // Typedef for Options pack.
68  typedef ::myra::multifrontal::Options Options;
69 
70  // ----------------------------------------- Construction, serialization, value semantics.
71 
74 
76  SparseLUSolver(const SparseLUSolver& that);
77 
79  void swap(SparseLUSolver& that);
80 
81 #ifdef MYRAMATH_ENABLE_CPP11
84 #endif
85 
87  SparseLUSolver& operator = (SparseLUSolver that);
88 
90  explicit SparseLUSolver(InputStream& in);
91 
93  void write(OutputStream& out) const;
94 
96  ~SparseLUSolver();
97 
98  // ----------------------------------------- Factorization and factorizing constructors.
99 
101  SparseLUSolver(const SRange& in_A, Options options = defaults());
102  void factor(const SRange& A, Options options = defaults());
103  JobGraph factor_jobgraph(const SRange& A, Options options = defaults());
104 
106  SparseLUSolver(const SRange& in_A, const Permutation& P, Options options = defaults());
107  void factor(const SRange& in_A, const Permutation& P, Options options = defaults());
108  JobGraph factor_jobgraph(const SRange& in_A, const Permutation& P, Options options = defaults());
109 
111  SparseLUSolver(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
112  void factor(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
113  JobGraph factor_jobgraph(const SRange& in_A, const AssemblyTree& tree, Options options = defaults());
114 
115  // ----------------------------------------- Linear solution.
116 
118  // side = Solve by A from the 'L'eft or from the 'R'ight?
119  // op = Apply an operation to A? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
120  void solve(const DRange& B, char side = 'L', char op = 'N', Options options = defaults()) const;
121  JobGraph solve_jobgraph(const DRange& B, char side = 'L', char op = 'N', Options options = defaults()) const;
122 
124  // side = Solve by L from the 'L'eft or from the 'R'ight?
125  // op = Apply an operation to L? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
126  void solveL(const DRange& B, char side, char op, Options options) const;
127  JobGraph solveL_jobgraph(const DRange& B, char side, char op, Options options) const;
128 
130  // side = Solve by U from the 'L'eft or from the 'R'ight?
131  // op = Apply an operation to U? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
132  void solveU(const DRange& B, char side, char op, Options options) const;
133  JobGraph solveU_jobgraph(const DRange& B, char side, char op, Options options) const;
134 
135  // ----------------------------------------- Solution with refinement.
136 
138  // side = Solve by A from the 'L'eft or from the 'R'ight?
139  // op = Apply an operation to A? ('T'ranspose, 'H'ermitian, 'C'onjugate or 'N'othing)
140  // Note, this is more expensive because it calls solve() multiple times, and sparse gemm() too.
141  // Returns the residual history, frobenius(A*X-B), evaluated at each refinement iteration.
142  std::vector<Precision> refine(const DRange& B, char side = 'L', char op = 'N', Precision tolerance = default_tolerance(), int iterations = default_iterations(), Options options = defaults()) const;
144  friend class SparseLUSolver_RefineAction;
145 
146  // ----------------------------------------- Calculating schur complements.
147 
149  // Note that in the complex case, ' denotes (non-conjugated) 'T'ranspose, not 'H'ermitian.
150  Matrix<Number> schur(const SRange& B, const SRange& C, Options options = defaults()) const;
151  void schur_inplace(const SRange& B, const SRange& C, const DRange& S, Options options = defaults()) const;
152  JobGraph schur_jobgraph(const SRange& B, const SRange& C, const DRange& S, Options options = defaults()) const;
153 
155  Matrix<Number> schur(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, Options options = defaults()) const;
156  void schur_inplace(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, const DRange& S, Options options = defaults()) const;
157  JobGraph schur_jobgraph(const iRange& Bi, const DRange& Bv, const iRange& Ci, const DRange& Cv, const DRange& S, Options options = defaults()) const;
158 
159  // ----------------------------------------- Sampling inv(A).
160 
162  Number inverse(int i, int j) const;
163 
165  Matrix<Number> inverse(const iRange& i, const iRange& j, Options options = defaults()) const;
166  void inverse_inplace(const iRange& i, const iRange& j, const DRange& Z, Options options = defaults()) const;
167  JobGraph inverse_jobgraph(const iRange& i, const iRange& j, const DRange& Z, Options options = defaults()) const;
168 
169  // ----------------------------------------- Partial linear solution.
170 
172  // Note X = partialsolve(linspace(0,N),linspace(0,N),B,side,op) yields the same result X = solve(B,side,op)
173  // Note X = partialsolve(i,j,B,'L'eft, op) yields the same result as X = op(this->inverse(i,j))*B
174  // Note X = partialsolve(i,j,B,'R'ight,op) yields the same result as X = B*op(this->inverse(i,j))
175  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;
176  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;
177  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;
178 
179  // ----------------------------------------- Miscellany.
180 
182  int size() const;
183 
185  const AssemblyTree& tree() const;
186 
188  static Options defaults();
189  static Precision default_tolerance();
190  static int default_iterations();
191 
192  private:
193 
194  // Numeric contents.
195  typedef LUKernel<Number> Kernel;
196  typedef ::myra::multifrontal::detail::lu::LUContainer<Kernel> LUContainer;
197  LUContainer LU;
198 
199  };
200 
202 template<class Number> class ReflectNumber <SparseLUSolver<Number> >
203  { public: typedef Number type; };
204 
206 MYRAMATH_EXPORT Vector<NumberS> operator * (const SparseLUSolver<NumberS>& solver, const CVectorRange<NumberS>& x);
208 MYRAMATH_EXPORT Vector<NumberD> operator * (const SparseLUSolver<NumberD>& solver, const CVectorRange<NumberD>& x);
209 MYRAMATH_EXPORT Vector<NumberC> operator * (const SparseLUSolver<NumberC>& solver, const CVectorRange<NumberC>& x);
210 MYRAMATH_EXPORT Vector<NumberZ> operator * (const SparseLUSolver<NumberZ>& solver, const CVectorRange<NumberZ>& x);
212 
214 MYRAMATH_EXPORT Matrix<NumberS> operator * (const SparseLUSolver<NumberS>& solver, const CMatrixRange<NumberS>& X);
216 MYRAMATH_EXPORT Matrix<NumberD> operator * (const SparseLUSolver<NumberD>& solver, const CMatrixRange<NumberD>& X);
217 MYRAMATH_EXPORT Matrix<NumberC> operator * (const SparseLUSolver<NumberC>& solver, const CMatrixRange<NumberC>& X);
218 MYRAMATH_EXPORT Matrix<NumberZ> operator * (const SparseLUSolver<NumberZ>& solver, const CMatrixRange<NumberZ>& X);
220 
221 } // namespace myra
222 
223 #endif
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
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
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Pivot factorization for SparseLUSolver.
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
Factors A into L*U, presents solve methods.
Definition: Kernel.h:35
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
Sparse direct solver suitable for symmetric-pattern nonsymmetric-valued A.
Definition: SparseLUSolver.h:57
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
Represents a const VectorRange.
Definition: axpy.h:20
Definition: partialsolve.h:32
Definition: SparseLUSolver.cpp:439
Represents a const intRange.
Definition: intRange.h:142