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
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 */
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
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
const Vector< T > const Vector< T > & x
Definition LM_fit.h:97
#define NAMESPACE_END
Definition basics.h:323
#define WEAK(x)
Definition basics.h:485
#define NAMESPACE_TBCI
Definition basics.h:317
#define U
Definition bdmatlib.cc:21
exception class: Use MatErr from matrix.h
Definition cscmatrix.h:66
Temporary Base Class Idiom: Class TVector is used for temporary variables.
Definition vector.h:73
Our own complex class.
Definition cplx.h:56
void get_perm_c(int, SuperMatrix *, int *)
void StatFree()
void Destroy_SuperMatrix_Store(SuperMatrix *)
void Destroy_CompCol_Matrix(SuperMatrix *)
int sp_ienv(int)
void StatInit(int, int)
void Destroy_SuperNode_Matrix(SuperMatrix *)
int * intMalloc(int)
const unsigned TMatrix< T > const Matrix< T > * a
#define doublecomplex
int expansions
Definition csp_defs.h:121
float total_needed
Definition csp_defs.h:120
float for_lu
Definition csp_defs.h:119
colperm_t
get column permutation vector perm_c[], according to permc_spec: permc_spec = NATURAL(0): use the nat...
Definition superlu.h:20
NAMESPACE_TBCI int lu_solve(CSCMatrix< cplx< double > > &M, Vector< cplx< double > > &x, const Vector< cplx< double > > &rhs, colperm_t permc_spec, bool verbose, bool symm)
Definition superlu_z.cpp:31
enums and structs for the SuperMatrix being used in SuperLU
@ SLU_GE
Definition supermatrix.h:32
@ SLU_Z
Definition supermatrix.h:28
@ SLU_NC
Definition supermatrix.h:14
@ SLU_DN
Definition supermatrix.h:21
#define SUPERLU_FREE(addr)
Definition util.h:37
#define ABORT(err_msg)
Definition util.h:21
int zQuerySpace(SuperMatrix *, SuperMatrix *, int, mem_usage_t *)
void zCreate_CompCol_Matrix(SuperMatrix *, int, int, int, doublecomplex *, int *, int *, Stype_t, Dtype_t, Mtype_t)
void zCreate_Dense_Matrix(SuperMatrix *, int, int, doublecomplex *, int, Stype_t, Dtype_t, Mtype_t)
void zgssv(SuperMatrix *, int *, int *, SuperMatrix *, SuperMatrix *, SuperMatrix *, int *)