OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
umfpack_solver.hpp
1 /*
2  * Umfpack_solver.hpp
3  *
4  * Created on: Nov 27, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_NUMERICS_SRC_SOLVERS_UMFPACK_SOLVER_HPP_
9 #define OPENFPM_NUMERICS_SRC_SOLVERS_UMFPACK_SOLVER_HPP_
10 
11 #define UMFPACK_NONE 0
12 
13 #define SOLVER_NOOPTION 0
14 #define SOLVER_PRINT_RESIDUAL_NORM_INFINITY 1
15 #define SOLVER_PRINT_DETERMINANT 2
16 
17 #if defined(HAVE_EIGEN) && defined(HAVE_SUITESPARSE)
18 
20 
21 #include "Vector/Vector.hpp"
22 #include "Eigen/UmfPackSupport"
23 #include <Eigen/SparseLU>
24 
25 
26 template<typename T>
27 class umfpack_solver
28 {
29 public:
30 
31  template<unsigned int impl, typename id_type> static Vector<T> solve(const SparseMatrix<T,id_type,impl> & A, const Vector<T> & b)
32  {
33  std::cerr << "Error Umfpack only support double precision, and int ad id type" << "/n";
34  }
35 
36  void best_solve()
37  {
38  std::cerr << "Error Umfpack only support double precision, and int ad id type" << "/n";
39  }
40 };
41 
42 
43 template<>
44 class umfpack_solver<double>
45 {
46 
47 public:
48 
58  static Vector<double,EIGEN_BASE> try_solve(SparseMatrix<double,int,EIGEN_BASE> & A, const Vector<double,EIGEN_BASE> & b, size_t opt = UMFPACK_NONE)
59  {
60  return solve(A,b,opt);
61  }
62 
73  {
74  Vcluster & vcl = create_vcluster();
75 
77 
78  // only master processor solve
79  Eigen::UmfPackLU<Eigen::SparseMatrix<double,0,int> > solver;
80 
81  // Collect the matrix on master
82  auto mat_ei = A.getMat();
83 
84  Eigen::Matrix<double, Eigen::Dynamic, 1> x_ei;
85 
86  // Collect the vector on master
87  auto b_ei = b.getVec();
88 
89  // Copy b into x, this also copy the information on how to scatter back the information on x
90  x = b;
91 
92  if (vcl.getProcessUnitID() == 0)
93  {
94  solver.compute(mat_ei);
95 
96  if(solver.info()!=Eigen::Success)
97  {
98  // decomposition failed
99  std::cout << __FILE__ << ":" << __LINE__ << " solver failed" << "\n";
100 
101  x.scatter();
102 
103  return x;
104  }
105 
106  x_ei = solver.solve(b_ei);
107 
108  if (opt & SOLVER_PRINT_RESIDUAL_NORM_INFINITY)
109  {
110  Eigen::Matrix<double, Eigen::Dynamic, 1> res;
111  res = mat_ei * x_ei - b_ei;
112 
113  std::cout << "Infinity norm: " << res.lpNorm<Eigen::Infinity>() << "\n";
114  }
115 
116  if (opt & SOLVER_PRINT_DETERMINANT)
117  {
118  std::cout << " Determinant: " << solver.determinant() << "\n";
119  }
120 
121  x = x_ei;
122  }
123 
124  // Vector is only on master, scatter back the information
125  x.scatter();
126 
127  return x;
128  }
129 };
130 
131 #else
132 
134 
135 #include "Vector/Vector.hpp"
136 
138 template<typename T>
140 {
141 public:
142 
144  template<typename impl> static Vector<T> solve(const SparseMatrix<T,impl> & A, const Vector<T> & b)
145  {
146  std::cerr << __FILE__ << ":" << __LINE__ << " Error Umfpack only support double precision" << "/n";
147  }
148 
150  void best_solve()
151  {
152  std::cerr << __FILE__ << ":" << __LINE__ << " Error Umfpack only support double precision" << "/n";
153  }
154 };
155 
157 template<>
158 class umfpack_solver<double>
159 {
160 
161 public:
162 
164  template<unsigned int impl, typename id_type> static Vector<double> solve(SparseMatrix<double,id_type,impl> & A, const Vector<double> & b, size_t opt = UMFPACK_NONE)
165  {
166  std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use umfpack you must compile OpenFPM with linear algebra support" << "/n";
167 
168  Vector<double> x;
169 
170  return x;
171  }
172 
174  void best_solve()
175  {
176  std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use umfpack you must compile OpenFPM with linear algebra support" << "/n";
177  }
178 };
179 
180 #endif
181 
182 
183 #endif /* OPENFPM_NUMERICS_SRC_SOLVERS_UMFPACK_SOLVER_HPP_ */
size_t getProcessUnitID()
Get the process unit id.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support...
Definition: Vector.hpp:39
static Vector< T > solve(const SparseMatrix< T, impl > &A, const Vector< T > &b)
stub solve
Implementation of VCluster class.
Definition: VCluster.hpp:36
int & getVec()
stub getVec
Definition: Vector.hpp:177
void scatter()
scatter
Definition: Vector.hpp:144
Sparse Matrix implementation.
static Vector< double > solve(SparseMatrix< double, id_type, impl > &A, const Vector< double > &b, size_t opt=UMFPACK_NONE)
stub solve
stub when library compiled without eigen
void best_solve()
stub solve
void best_solve()
stub solve