MyraMath
zldlt2_solver


Source: tests/multifrontal/zldlt/zldlt2_solver.cpp

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 
11 // Containers.
13 #include <myramath/dense/Matrix.h>
15 #include <myramath/dense/Vector.h>
20 
21 // Algorithms.
23 #include <myramath/sparse/gemm.h>
24 #include <myramath/sparse/gemv.h>
26 #include <myramath/sparse/phasor.h>
28 
29 // Solver type under test.
31 
32 // For testing serialization roundtrip.
34 
35 // Reporting.
37 #include <tests/myratest.h>
38 
39 using namespace myra;
40 using namespace myra_stlprint;
41 
42 namespace {
43 
44 template<class Precision> void test(int I, int J, Precision tolerance)
45  {
46  myra::out() << typestring<Precision>() << std::endl;
47  typedef std::complex<Precision> Number;
48  int N = I*J;
49  // Make laplacian A, shift it indefinite, phase shift to make it nontrivially complex-symmetric.
50  SparseMatrix<Number> A = laplacian2<Number>(I,J);
51  Number shift = 5.5;
52  for (int n = 0; n < N; ++n)
53  A(n,n) -= shift;
54  phasor_symmetric(A);
55  // Factor A = L*L'
56  typedef SparseZLDLTSolver<Precision> Solver;
57  Solver solver(A);
58  // Note that this indefinite solver is not very accurate in
59  // single precision, so we only check/throw residual errors
60  // when we're using backwards refinement with them. Note this
61  // refinement is baked into SparseZLDLT.refine() automatically.
62  // Test A*x = b, no refinement.
63  {
64  auto b = Vector<Number>::random(N);
65  auto x = b;
66  solver.solve(x.column(),'L','N');
67  Precision residual = frobenius(A*x-b) / frobenius(b);
68  myra::out() << " |A*x-b| (solve) = " << residual << std::endl;
69  // Note, we don't check the residual on this one.
70  // REQUIRE(residual < tolerance);
71  }
72  // Test A*x = b, using refine()
73  {
74  auto b = Vector<Number>::random(N);
75  auto x = b;
76  auto history = solver.refine(x.column(),'L','N');
77  Precision residual = frobenius(A*x-b) / frobenius(b);
78  myra::out() << " |A*x-b| (refine) = " << residual << std::endl;
79  myra::out() << " history = " << history << std::endl;
80  // Note, we do check the residual on this one (and all the rest)
81  REQUIRE(residual < tolerance);
82  }
83  // Test hermitian(A)*x = b, using refine()
84  {
85  auto b = Vector<Number>::random(N);
86  auto x = b;
87  auto history = solver.refine(x.column(),'L','H');
88  Precision residual = frobenius(hermitian(A)*x-b) / frobenius(b);
89  myra::out() << " |A^H*x-b| (refine) = " << residual << std::endl;
90  myra::out() << " history = " << history << std::endl;
91  REQUIRE(residual < tolerance);
92  }
93  // Test x*A = b, using refine()
94  {
95  auto b = Vector<Number>::random(N);
96  auto x = b;
97  auto history = solver.refine(x.row(),'R','N');
98  Precision residual = frobenius(x*A-b) / frobenius(b);
99  myra::out() << " |x*A-b| (refine) = " << residual << std::endl;
100  REQUIRE(residual < tolerance);
101  }
102  // Test x*hermitian(A) = b, using refine()
103  {
104  auto b = Vector<Number>::random(N);
105  auto x = b;
106  auto history = solver.refine(x.row(),'R','H');
107  Precision residual = frobenius(x*hermitian(A)-b) / frobenius(b);
108  myra::out() << " |x*A^H-b| (refine) = " << residual << std::endl;
109  REQUIRE(residual < tolerance);
110  }
111  // Make a deep copy of solver, test A*x = b again.
112  {
113  Solver copy = solver;
114  auto b = Vector<Number>::random(N);
115  auto x = b;
116  auto history = solver.refine(x.column(),'L','N');
117  Precision residual = frobenius(A*x-b) / frobenius(b);
118  myra::out() << " |inv(A)*b-x| (copy) = " << residual << std::endl;
119  REQUIRE(residual < tolerance);
120  }
121  // Roundtrip solver to file, test A*x = b again.
122  {
123  VectorStream inout;
124  solver.write(inout);
125  Solver saved(inout);
126  auto b = Vector<Number>::random(N);
127  auto x = b;
128  auto history = saved.refine(x.column(),'L','N');
129  Precision residual = frobenius(A*x-b) / frobenius(b);
130  myra::out() << " |A*x-b| (saved) = " << residual << std::endl;
131  REQUIRE(residual < tolerance);
132  }
133  }
134 
135 } // namespace
136 
137 ADD_TEST("zldlt2_solver","[multifrontal][parallel]")
138  {
139  // Define problem size.
140  int I = 64;
141  int J = 64;
142  int N = I*J;
143  // Run tests.
144  test<float >(I,J,1.0e-4f);
145  test<double>(I,J,1.0e-8);
146  }
Sparse direct solver suitable for complex symmetric systems.
Definition: SparseZLDLTSolver.h:61
Interface class for representing subranges of dense Matrix&#39;s.
Interface class for representing subranges of dense Vector&#39;s.
Variety of routines for mixed dense*sparse or dense*sparse matrix multiplies. The dense*dense case is...
Routines for computing Frobenius norms of various algebraic containers.
Sparse direct solver suitable for complex symmetric systems.
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
General purpose compressed-sparse-column (CSC) container.
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
Definition: syntax.dox:1
Definition: stlprint.h:32
Routines for printing the contents of various std::container&#39;s to a std::ostream using operator <<...
Various utility functions/classes related to scalar Number types.
Signatures for sparse matrix * dense vector multiplies. All delegate to gemm() under the hood...
A stream that serialize/deserializes to std::vector<char> buffer.
General purpose dense matrix container, O(i*j) storage.
Container for either a column vector or row vector (depends upon the usage context) ...
Aggregates a (perm, iperm, swaps) triple into a vocabulary type.
Returns a hermitian copy of a SparseMatrix.
Stores an IxJ matrix A in compressed sparse column format.
Definition: bothcat.h:23
Helper routines for reordering/filling 2D structured grids. Used by many unit tests.
Applies random phase shifts to a complex square SparseMatrix.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

float
|A*x-b| (solve) = 0.000470822
|A*x-b| (refine) = 1.12287e-05
history = [ 0.000818973 1.12633e-05 ] (2)
|A^H*x-b| (refine) = 8.28294e-06
history = [ 0.000873155 8.3314e-06 ] (2)
|x*A-b| (refine) = 8.83332e-06
|x*A^H-b| (refine) = 4.43869e-06
|inv(A)*b-x| (copy) = 1.25217e-05
|A*x-b| (saved) = 9.45063e-06
double
|A*x-b| (solve) = 1.59198e-12
|A*x-b| (refine) = 1.40318e-14
history = [ 3.83509e-12 1.40993e-14 ] (2)
|A^H*x-b| (refine) = 1.74107e-14
history = [ 2.37101e-12 1.75394e-14 ] (2)
|x*A-b| (refine) = 8.6354e-15
|x*A^H-b| (refine) = 1.92932e-14
|inv(A)*b-x| (copy) = 1.91602e-14
|A*x-b| (saved) = 1.33625e-14


Go back to Summary of /test programs.