GetFEM++  5.3
getfem_superlu.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2017 Julien Pommier
5 
6  This file is a part of GetFEM++
7 
8  GetFEM++ is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file getfem_superlu.h
33  @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
34  @date August 2004.
35  @brief SuperLU interface for getfem
36 
37  We do not use gmm_superlu_interface.h for a good reason. This file
38  does not include any of the superlu headers, hence when getfem is
39  installed, it does not need to install the superlu headers.
40 */
41 
42 #ifndef GETFEM_SUPERLU
43 #define GETFEM_SUPERLU
44 #ifndef GMM_USES_SUPERLU
45 #define GMM_USES_SUPERLU
46 #endif
47 #include "getfem_config.h"
48 #include "gmm/gmm_kernel.h"
49 
50 namespace gmm {
51 
52  template<typename T>
53  int SuperLU_solve(const gmm::csc_matrix<T> &A, T *X_, T *B, double& rcond_, int permc_spec = 3);
54  /** solve a sparse linear system AX=B (float, double, complex<float>
55  or complex<double>) via SuperLU.
56 
57  @param A the matrix (a copy is made if A is not a gmm::csc_matrix)
58  @param X the solution.
59  @param B the right hand side.
60  @param rcond_ contains on output an estimate of the condition number of A.
61  @param permc_spec specify the kind of renumbering than SuperLU should do.
62  */
63  template<typename MAT, typename V1, typename V2>
64  int SuperLU_solve(const MAT &A, const V1& X, const V2& B, double& rcond_, int permc_spec = 3) {
65  typedef typename gmm::linalg_traits<MAT>::value_type T;
66 
67  int m = int(mat_nrows(A)), n = int(mat_ncols(A));
68  gmm::csc_matrix<T> csc_A(m,n);
69  gmm::copy(A,csc_A);
70  std::vector<T> rhs(m), sol(m);
71  gmm::copy(B, rhs);
72 
73  int info = SuperLU_solve(csc_A, &sol[0], &rhs[0], rcond_, permc_spec);
74  gmm::copy(sol, const_cast<V1 &>(X));
75  return info;
76  }
77 
78  struct SuperLU_factor_impl_common;
79 
80  /** Factorization of a sparse matrix with SuperLU.
81 
82  This class can be used as a preconditioner for gmm iterative solvers.
83  */
84  template <class T> class SuperLU_factor {
85  std::shared_ptr<SuperLU_factor_impl_common> impl;
86  public :
87  enum { LU_NOTRANSP, LU_TRANSP, LU_CONJUGATED };
88 
89  /** Do the factorization of the supplied sparse matrix. */
90  template <class MAT> void build_with(const MAT &A, int permc_spec = 3) {
91  int m = int(mat_nrows(A)), n = int(mat_ncols(A));
92  gmm::csc_matrix<T> csc_A(m,n);
93  gmm::copy(A,csc_A);
94  build_with(csc_A, permc_spec);
95  }
96  void build_with(const gmm::csc_matrix<T> &A, int permc_spec = 3);
97  template <typename VECTX, typename VECTB>
98  /** After factorization, do the triangular solves.
99  transp = LU_NOTRANSP -> solves Ax = B
100  transp = LU_TRANSP -> solves A'x = B
101  transp = LU_CONJUGATED -> solves conj(A)X = B
102  */
103  void solve(const VECTX &X, const VECTB &B, int transp=LU_NOTRANSP) const {
104  gmm::copy(B, rhs());
105  solve(transp);
106  gmm::copy(sol(),const_cast<VECTX &>(X));
107  }
108  void solve(int transp=LU_NOTRANSP) const;
109  std::vector<T> &sol() const;
110  std::vector<T> &rhs() const;
111  SuperLU_factor();
112  float memsize() const;
113  SuperLU_factor(const SuperLU_factor& other);
114  SuperLU_factor& operator=(const SuperLU_factor& other);
115  };
116 
117  template <typename T, typename V1, typename V2> inline
118  void mult(const SuperLU_factor<T>& P, const V1 &v1, const V2 &v2) {
119  P.solve(v2,v1);
120  }
121 
122  template <typename T, typename V1, typename V2> inline
123  void transposed_mult(const SuperLU_factor<T>& P,const V1 &v1,const V2 &v2) {
125  }
126 }
127 
128 extern "C" void set_superlu_callback(int (*cb)());
129 
130 #endif
void build_with(const MAT &A, int permc_spec=3)
Do the factorization of the supplied sparse matrix.
Factorization of a sparse matrix with SuperLU.
Include the base gmm files.
void solve(const VECTX &X, const VECTB &B, int transp=LU_NOTRANSP) const
After factorization, do the triangular solves.
defines and typedefs for namespace getfem