TBCI Numerical high perf. C++ Library  2.8.0
superlu_z.cpp
Go to the documentation of this file.
1 
4 /* (w) Andreas Ahland */
5 /* $Id: superlu_z.cpp,v 1.4.2.16 2022/11/03 20:40:08 garloff Exp $ */
6 
7 #include "tbci/cscmatrix.h"
8 #include "tbci/cplx.h"
9 
10 #ifdef HAVE_SUPERLU3_INCLUDES
11 # include <superlu/slu_zdefs.h>
12 # include <superlu/supermatrix.h>
13 # include <superlu/slu_util.h>
14 #else
15 # include "tbci/superlu/util.h"
16 # include "tbci/superlu/zsp_defs.h"
17 #endif
18 
19 #if TBCI_NUMLIB_VERSION >= 20600
20 # define COLPERM_T_DECLARED
21 # include "tbci/solver/superlu.h"
22 #endif
23 
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
29 
30 
32  Vector< cplx<double> >& x,
33  const Vector< cplx<double> >& rhs,
34  colperm_t permc_spec,
35  bool verbose, bool symm)
36 {
37  int nrhs = 1;
38  int m = M.rows();
39  int n = M.columns();
40  int nnz = M.size();
41  doublecomplex *a= (doublecomplex*) M.dataPointer();
42  unsigned int *asub= M.rowIndexPointer();
43  unsigned int *xa = M.columnPointer();
44 
45  int *perm_r; /* row permutations from partial pivoting */
46  int *perm_c; /* column permutation vector */
47  SuperMatrix A;
48  SuperMatrix B;
49  SuperMatrix L; /* factor L */
50  SuperMatrix U; /* factor U */
51  //char trans[1];
52  int info;
53 
54 #ifdef HAVE_SUPERLU3_INCLUDES
55  superlu_options_t slu_opt;
56  SuperLUStat_t slu_stat;
57  set_default_options(&slu_opt);
58  slu_opt.ColPerm = permc_spec;
59  slu_opt.SymmetricMode = (yes_no_t)symm;
60  if (symm)
61  slu_opt.DiagPivotThresh = 0.1;
62  slu_opt.PrintStat = (yes_no_t)verbose;
63  StatInit(&slu_stat);
64 #elif defined(HAVE_UNISTD_H)
65  int savedstdout = 0;
66 #endif
67 
68  // Set up matrix structure
69  x = rhs;
70  //*trans = 'N';
71  zCreate_CompCol_Matrix(&A, m, n, nnz, a, (int*)asub, (int*)xa, SLU_NC, SLU_Z, SLU_GE);
72  zCreate_Dense_Matrix(&B, m, nrhs, (doublecomplex*) x.get_fortran_vector(),
73  m, SLU_DN, SLU_Z, SLU_GE);
74  if ( !(perm_r = intMalloc(m)) )
75  ABORT("Malloc fails for perm_r[].");
76  if ( !(perm_c = intMalloc(n)) )
77  ABORT("Malloc fails for perm_c[].");
78 
79 #if defined(HAVE_UNISTD_H) && !defined(HAVE_SUPERLU3_INCLUDES)
80  // HACK: SuperLU is way too talkative. Shut it down ...
81  if (!verbose) {
82  savedstdout = dup(STDOUT_FILENO); close(STDOUT_FILENO);
83  }
84 #endif
85 #ifndef HAVE_SUPERLU3_INCLUDES
86  // Perform permutation
87  get_perm_c(permc_spec, &A, perm_c);
88 #endif
89 
90  // Solve
91 #ifdef HAVE_SUPERLU3_INCLUDES
92  zgssv(&slu_opt, &A, perm_c, perm_r, &L, &U, &B, &slu_stat, &info);
93  if (verbose)
94  StatPrint(&slu_stat);
95 #else
96  zgssv(&A, perm_c, perm_r, &L, &U, &B, &info);
97 #endif
98 #if defined(HAVE_UNISTD_H) && !defined(HAVE_SUPERLU3_INCLUDES)
99  // Restore stdout FD
100  if (!verbose) {
101  dup2(savedstdout, STDOUT_FILENO); close(savedstdout);
102  }
103 #endif
104 
105  if (info != 0) {
106  fprintf(stderr, "SuperLU: zgssv() error returns INFO= %d\n", info);
107  if (info > n) {
108  /* We ran out of memory */
109  mem_usage_t mem_usage;
110 #ifdef HAVE_SUPERLU3_INCLUDES
111  zQuerySpace(&L, &U, &mem_usage);
112 #else
113  int panel_size = sp_ienv(1);
114  zQuerySpace(&L, &U, panel_size, &mem_usage);
115 #endif
116  fprintf(stderr, "SuperLU: Memory allocation of %i bytes failed!\n",
117  info - n);
118  fprintf(stderr, " L\\U MB %.3f\ttotal MB needed %.3f",
119  mem_usage.for_lu/1e6, mem_usage.total_needed/1e6);
120 #ifdef HAVE_MEM_USAGE_EXPANSIONS
121  fprintf(stderr, "\texpansions %d\n",
122  mem_usage.expansions);
123 #else
124  fprintf(stderr, "\n");
125 #endif
126  } else
127  fprintf(stderr, "SuperLU: Singular factor U(%i,%i) found!\n", info, info);
128  }
129 
130  SUPERLU_FREE(perm_r);
131  SUPERLU_FREE(perm_c);
134  // Huh, memleak :-O
137 #ifdef HAVE_SUPERLU3_INCLUDES
138  StatFree(&slu_stat);
139 #endif
140 
141  return info;
142 }
143 
144 #if TBCI_NUMLIB_VERSION < 20600
145 
150  const Vector< cplx<double> >& rhs,
151  colperm_t permc_spec,
152  bool verbose, bool symm)
153 )
154 {
155  TVector< cplx<double> > x(rhs.size());
156  const int info = lu_solve(M, (Vector< cplx<double> >&)x,
157  rhs, permc_spec, verbose, symm);
158  return x;
159 }
160 #endif
void Destroy_SuperMatrix_Store(SuperMatrix *)
const Vector< T > const Vector< T > & x
Definition: LM_fit.h:97
void zCreate_Dense_Matrix(SuperMatrix *, int, int, doublecomplex *, int, Stype_t, Dtype_t, Mtype_t)
int * intMalloc(int)
void StatInit(int, int)
Our own complex class.
Definition: cplx.h:48
#define NAMESPACE_TBCI
Definition: basics.h:317
float for_lu
Definition: csp_defs.h:119
void zgssv(SuperMatrix *, int *, int *, SuperMatrix *, SuperMatrix *, SuperMatrix *, int *)
F_TMatrix< double > lu_solve(const F_Matrix< double > &A, const F_Matrix< double > &B, int overwriteA=0)
Definition: lapack.cpp:156
#define SUPERLU_FREE(addr)
Definition: util.h:37
void zCreate_CompCol_Matrix(SuperMatrix *, int, int, int, doublecomplex *, int *, int *, Stype_t, Dtype_t, Mtype_t)
#define WEAK(x)
Definition: basics.h:485
enums and structs for the SuperMatrix being used in SuperLU
colperm_t
get column permutation vector perm_c[], according to permc_spec: permc_spec = NATURAL(0): use the nat...
Definition: superlu.h:20
float total_needed
Definition: csp_defs.h:120
#define U
Definition: bdmatlib.cc:21
void Destroy_CompCol_Matrix(SuperMatrix *)
void get_perm_c(int, SuperMatrix *, int *)
void Destroy_SuperNode_Matrix(SuperMatrix *)
int sp_ienv(int)
int expansions
Definition: csp_defs.h:121
exception class: Use MatErr from matrix.h
Definition: cscmatrix.h:49
Temporary Base Class Idiom: Class TVector is used for temporary variables.
Definition: bvector.h:52
#define ABORT(err_msg)
Definition: util.h:21
#define NAMESPACE_END
Definition: basics.h:323
int zQuerySpace(SuperMatrix *, SuperMatrix *, int, mem_usage_t *)
Definition: bvector.h:54
const unsigned TMatrix< T > const Matrix< T > * a
void StatFree()