TBCI Numerical high perf. C++ Library 2.8.0
superlu_stdcplx.cpp
Go to the documentation of this file.
1
4/* (w) Andreas Ahland */
5/* $Id: superlu_stdcplx.cpp,v 1.1.2.18 2022/11/03 20:40:08 garloff Exp $ */
6
7#include "tbci/cscmatrix.h"
8#include "tbci/std_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_stdcplx.h"
22#endif
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27
29
32 const Vector< CPLX__ complex<double> >& rhs,
33 colperm_t permc_spec,
34 bool verbose, bool symm)
35{
36 int nrhs = 1;
37 int m = M.rows();
38 int n = M.columns();
39 int nnz = M.size();
40 doublecomplex *a= (doublecomplex*) M.dataPointer();
41 unsigned int *asub= M.rowIndexPointer();
42 unsigned int *xa = M.columnPointer();
43
44 int *perm_r; /* row permutations from partial pivoting */
45 int *perm_c; /* column permutation vector */
48 SuperMatrix L; /* factor L */
49 SuperMatrix U; /* factor U */
50 //char trans[1];
51 int info;
52#ifdef HAVE_SUPERLU3_INCLUDES
53 superlu_options_t slu_opt;
54 SuperLUStat_t slu_stat;
55 set_default_options(&slu_opt);
56 slu_opt.ColPerm = permc_spec;
57 slu_opt.SymmetricMode = (yes_no_t)symm;
58 if (symm)
59 slu_opt.DiagPivotThresh = 0.1;
60 slu_opt.PrintStat = (yes_no_t)verbose;
61 StatInit(&slu_stat);
62#elif defined(HAVE_UNISTD_H)
63 int savedstdout = 0;
64#endif
65
66 // Set up matrix structure
67 x = rhs;
68 //*trans = 'N';
69 zCreate_CompCol_Matrix(&A, m, n, nnz, a, (int*)asub, (int*)xa, SLU_NC, SLU_Z, SLU_GE);
70 zCreate_Dense_Matrix(&B, m, nrhs, (doublecomplex*) x.get_fortran_vector(),
71 m, SLU_DN, SLU_Z, SLU_GE);
72 if ( !(perm_r = intMalloc(m)) )
73 ABORT("Malloc fails for perm_r[].");
74 if ( !(perm_c = intMalloc(n)) )
75 ABORT("Malloc fails for perm_c[].");
76
77#if defined(HAVE_UNISTD_H) && !defined(HAVE_SUPERLU3_INCLUDES)
78 // HACK: SuperLU is way too talkative. Shut it down ...
79 if (!verbose) {
80 savedstdout = dup(STDOUT_FILENO); close(STDOUT_FILENO);
81 }
82#endif
83#ifndef HAVE_SUPERLU3_INCLUDES
84 // Perform permutation
85 get_perm_c(permc_spec, &A, perm_c);
86#endif
87
88 // Solve
89#ifdef HAVE_SUPERLU3_INCLUDES
90 zgssv(&slu_opt, &A, perm_c, perm_r, &L, &U, &B, &slu_stat, &info);
91 if (verbose)
92 StatPrint(&slu_stat);
93#else
94 zgssv(&A, perm_c, perm_r, &L, &U, &B, &info);
95#endif
96#if defined(HAVE_UNISTD_H) && !defined(HAVE_SUPERLU3_INCLUDES)
97 // Restore stdout FD
98 if (!verbose) {
99 int newout = dup(savedstdout); close(savedstdout);
100 if (newout != STDOUT_FILENO)
101 fprintf(stderr, "Could not reopen stdout (%i)\n", newout);
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__ complex<double> >& rhs,
151 colperm_t permc_spec,
152 bool verbose, bool symm)
153)
154{
156 const int info = lu_solve(M, (Vector< CPLX__ complex<double> >&)x,
157 rhs, permc_spec, verbose, symm);
158 return x;
159}
160#endif
161
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
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 complex
#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__ complex< double > > &M, Vector< CPLX__ complex< double > > &x, const Vector< CPLX__ complex< double > > &rhs, colperm_t permc_spec, bool verbose, bool symm)
SuperLU solver wrapper for std::complex numbers.
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 *)