| 1 |
// for automatic compilation with ff-c++ |
| 2 |
//ff-c++-LIBRARY-dep: dissection blas pthread |
| 3 |
//ff-c++-cpp-dep: |
| 4 |
// |
| 5 |
// file to add Dissection solver with dynamic load. |
| 6 |
#include <iostream> |
| 7 |
#include <list> |
| 8 |
using namespace std; |
| 9 |
|
| 10 |
#include "rgraph.hpp" |
| 11 |
#include "error.hpp" |
| 12 |
#include "AFunction.hpp" |
| 13 |
#include "ff++.hpp" |
| 14 |
|
| 15 |
#include "MatriceCreuse_tpl.hpp" |
| 16 |
|
| 17 |
#include <wchar.h> |
| 18 |
|
| 19 |
#include "Dissection.hpp" |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
int generate_CSR(list<int>* ind_cols_tmp, list<T>* val_tmp, |
| 23 |
int nrow, int nrow1, int *old2new, int *new2old, |
| 24 |
int *ptrow, int *ind_col, T* val) |
| 25 |
{ |
| 26 |
int nnz = 0; |
| 27 |
// ind_cols_tmp = new list<int>[nrow]; |
| 28 |
// val_tmp = new list<T>[nrow]; |
| 29 |
for (int i = 0; i < nrow1; i++) { |
| 30 |
const int ii = new2old[i]; |
| 31 |
bool diag_flag = false; |
| 32 |
for (int k = ptrow[ii]; k < ptrow[ii + 1]; k++) { |
| 33 |
const int j = old2new[ind_col[k]]; |
| 34 |
if (i == j) { |
| 35 |
diag_flag = true; |
| 36 |
} |
| 37 |
// fprintf(stderr, "%d %d -> %d %d \n", i0, j0, ii, jj); |
| 38 |
if (ind_cols_tmp[i].empty()) { |
| 39 |
ind_cols_tmp[i].push_back(j); |
| 40 |
val_tmp[i].push_back(val[k]); |
| 41 |
nnz++; |
| 42 |
} |
| 43 |
else { |
| 44 |
if (ind_cols_tmp[i].back() < j) { |
| 45 |
ind_cols_tmp[i].push_back(j); |
| 46 |
val_tmp[i].push_back(val[k]); |
| 47 |
nnz++; |
| 48 |
} |
| 49 |
else { |
| 50 |
typename list<T>::iterator iv = val_tmp[i].begin(); |
| 51 |
list<int>::iterator it = ind_cols_tmp[i].begin(); |
| 52 |
for ( ; it != ind_cols_tmp[i].end(); ++it, ++iv) { |
| 53 |
if (*it == j) { |
| 54 |
fprintf(stderr, "already exits? (%d %d)\n", ii, j); |
| 55 |
break; |
| 56 |
} |
| 57 |
if (*it > j) { |
| 58 |
ind_cols_tmp[i].insert(it, j); |
| 59 |
val_tmp[i].insert(iv, val[k]); |
| 60 |
nnz++; |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} // loop : k |
| 67 |
if (!diag_flag) { |
| 68 |
fprintf(stderr, "%s %d : adding zero-entry %d\n", |
| 69 |
__FILE__, __LINE__, i); |
| 70 |
typename list<T>::iterator iv = val_tmp[i].begin(); |
| 71 |
list<int>::iterator it = ind_cols_tmp[i].begin(); |
| 72 |
for ( ; it != ind_cols_tmp[i].end(); ++it, ++iv) { |
| 73 |
if ((*it) > i) { |
| 74 |
ind_cols_tmp[i].insert(it, i); |
| 75 |
val_tmp[i].insert(iv, 0.0); |
| 76 |
nnz++; |
| 77 |
break; |
| 78 |
} |
| 79 |
} // loop : iv |
| 80 |
} // if (!diag_flag) |
| 81 |
} // loop : i |
| 82 |
return nnz; |
| 83 |
} |
| 84 |
|
| 85 |
template |
| 86 |
int generate_CSR<double>(list<int>* ind_cols_tmp, list<double>* val_tmp, |
| 87 |
int nrow, int nrow1, int *old2new, int *new2old, |
| 88 |
int *ptrow, int *ind_col, double * val); |
| 89 |
|
| 90 |
template |
| 91 |
int generate_CSR<Complex>(list<int>* ind_cols_tmp, |
| 92 |
list<Complex>* val_tmp, |
| 93 |
int nrow, int nrow1, int *old2new, |
| 94 |
int *new2old, |
| 95 |
int *ptrow, int *ind_col, |
| 96 |
Complex * val); |
| 97 |
|
| 98 |
template<typename T> |
| 99 |
int copy_CSR(int *ptrows, int *indcols, T* coefs, int nrow, |
| 100 |
list<int>* ind_cols_tmp, list<T>* val_tmp, |
| 101 |
int lower, int upper) |
| 102 |
{ |
| 103 |
ptrows[0] = 0; |
| 104 |
int k = 0; |
| 105 |
for (int i = 0; i < nrow; i++) { |
| 106 |
list<int>::iterator it = ind_cols_tmp[i].begin(); |
| 107 |
typename list<T>::iterator iv = val_tmp[i].begin(); |
| 108 |
for ( ; it != ind_cols_tmp[i].end(); ++it, ++iv) { |
| 109 |
if ((*it) >= lower && (*it) < upper) { |
| 110 |
indcols[k] = *it; |
| 111 |
coefs[k] = *iv; |
| 112 |
k++; |
| 113 |
} |
| 114 |
} |
| 115 |
ptrows[i + 1] = k; |
| 116 |
} // loop : i |
| 117 |
return k; |
| 118 |
} |
| 119 |
|
| 120 |
template |
| 121 |
int copy_CSR<double>(int *ptrows, int *indcols, double* coefs, int nrow, |
| 122 |
list<int>* ind_cols_tmp, list<double>* val_tmp, |
| 123 |
int lower, int upper); |
| 124 |
template |
| 125 |
int copy_CSR<Complex>(int *ptrows, int *indcols, |
| 126 |
Complex* coefs, int nrow, |
| 127 |
list<int>* ind_cols_tmp, |
| 128 |
list<Complex>* val_tmp, |
| 129 |
int lower, int upper); |
| 130 |
|
| 131 |
template<class R> |
| 132 |
class SolveDissection: public MatriceMorse<R>::VirtualSolver { |
| 133 |
double _tgv; |
| 134 |
double _pivot; |
| 135 |
int _dim0; |
| 136 |
int *_new2old; |
| 137 |
int *_ptrows0; |
| 138 |
int *_indcols0; |
| 139 |
double *_coefs0; |
| 140 |
int *_ptrows1; |
| 141 |
int *_indcols1; |
| 142 |
double *_coefs1; |
| 143 |
double *_xtmp; |
| 144 |
uint64_t *_dslv; |
| 145 |
public: |
| 146 |
|
| 147 |
SolveDissection(const MatriceMorse<R> &A, |
| 148 |
int strategy_, double ttgv, |
| 149 |
double pivot) : |
| 150 |
_tgv(ttgv), |
| 151 |
_pivot(pivot) |
| 152 |
{ |
| 153 |
const int dim = A.n; |
| 154 |
int num_threads = 1; |
| 155 |
if (getenv("DISSECTION_NUM_THREADS")) { |
| 156 |
sscanf(getenv("DISSECTION_NUM_THREADS"), "%d", &num_threads); |
| 157 |
fprintf(stderr, |
| 158 |
"environmental variable DISSECTION_NUM_THREADS = %d\n", |
| 159 |
num_threads); |
| 160 |
} |
| 161 |
_dslv = new uint64_t; |
| 162 |
diss_init(*_dslv, 0, 1, num_threads, ((verbosity > 3) ? 1 : 0)); |
| 163 |
// real matrix, with double precision factorization, # of threads |
| 164 |
const int decomposer = (strategy_ % 100) / 10; |
| 165 |
const int scaling = strategy_ == 0 ? 2 : strategy_ % 10; |
| 166 |
// sym + lower + isWhole = 1 + 2 + 4 |
| 167 |
const int sym = (strategy_ / 100) ? 5 : 0; |
| 168 |
|
| 169 |
int *old2new = new int[dim]; |
| 170 |
_new2old = new int[dim]; |
| 171 |
{ |
| 172 |
int m = 0; |
| 173 |
for (int i = 0; i < dim; i++) { |
| 174 |
for (int k = (A.lg)[i]; k < (A.lg)[i + 1]; k++) { |
| 175 |
const int j = (A.cl)[k]; |
| 176 |
if (i == j) { |
| 177 |
if ((A.a)[k] != ttgv) { |
| 178 |
_new2old[m] = i; |
| 179 |
m++; |
| 180 |
} |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
_dim0 = m; |
| 186 |
for (int i = 0; i < dim; i++) { |
| 187 |
for (int k = (A.lg)[i]; k < (A.lg)[i + 1]; k++) { |
| 188 |
const int j = (A.cl)[k]; |
| 189 |
if (i == j) { |
| 190 |
if ((A.a)[k] == ttgv) { |
| 191 |
_new2old[m] = i; |
| 192 |
m++; |
| 193 |
} |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
if (verbosity > 10) { |
| 199 |
cout << "m = " << m << " dim = " << dim << endl; |
| 200 |
} |
| 201 |
} |
| 202 |
for (int i = 0; i < dim; i++) { |
| 203 |
old2new[_new2old[i]] = i; |
| 204 |
} |
| 205 |
// |
| 206 |
_xtmp = new double[_dim0]; |
| 207 |
int nnz; |
| 208 |
list<int> *indcols_tmp = new list<int>[_dim0]; |
| 209 |
list<double> *coefs_tmp = new list<double>[_dim0]; |
| 210 |
nnz = generate_CSR<double>(indcols_tmp, coefs_tmp, dim, _dim0, old2new, |
| 211 |
_new2old, |
| 212 |
(int *)A.lg, (int *)A.cl, (double *)A.a); |
| 213 |
delete [] old2new; |
| 214 |
if (verbosity > 10) { |
| 215 |
cout << "nnz " << nnz << endl; |
| 216 |
} |
| 217 |
_ptrows0 = new int[_dim0 + 1]; |
| 218 |
_ptrows1 = new int[_dim0 + 1]; |
| 219 |
int *indcol_tmp = new int[nnz]; |
| 220 |
double *coef_tmp = new double[nnz]; |
| 221 |
int nnz1; |
| 222 |
nnz1 = copy_CSR<double>(_ptrows0, indcol_tmp, coef_tmp, _dim0, indcols_tmp, |
| 223 |
coefs_tmp, 0, _dim0); |
| 224 |
if (verbosity > 10) { |
| 225 |
cout << "nnz1 " << nnz1 << endl; |
| 226 |
} |
| 227 |
_indcols0 = new int[nnz1]; |
| 228 |
_coefs0 = new double[nnz1]; |
| 229 |
for (int i = 0; i < nnz1; i++) { |
| 230 |
_indcols0[i] = indcol_tmp[i]; |
| 231 |
_coefs0[i] = coef_tmp[i]; |
| 232 |
} |
| 233 |
nnz1 = copy_CSR<double>(_ptrows1, indcol_tmp, coef_tmp, _dim0, |
| 234 |
indcols_tmp, coefs_tmp, _dim0, dim); |
| 235 |
if (verbosity > 10) { |
| 236 |
cout << "nnz1 " << nnz1 << endl; |
| 237 |
} |
| 238 |
_indcols1 = new int[nnz1]; |
| 239 |
_coefs1 = new double[nnz1]; |
| 240 |
for (int i = 0; i < nnz1; i++) { |
| 241 |
_indcols1[i] = indcol_tmp[i]; |
| 242 |
_coefs1[i] = coef_tmp[i]; |
| 243 |
} |
| 244 |
|
| 245 |
delete [] indcols_tmp; |
| 246 |
delete [] coefs_tmp; |
| 247 |
delete [] indcol_tmp; |
| 248 |
delete [] coef_tmp; |
| 249 |
diss_s_fact(*_dslv, _dim0, _ptrows0, _indcols0, sym, decomposer); |
| 250 |
const int indefinite_flag = 1; |
| 251 |
const double eps_pivot = _pivot == (-1.0) ? 1.0e-2 : _pivot; |
| 252 |
diss_n_fact(*_dslv, _coefs0, scaling, eps_pivot, |
| 253 |
indefinite_flag); |
| 254 |
int n0; |
| 255 |
diss_get_kern_dim(*_dslv, &n0); |
| 256 |
if (n0 > 0) { |
| 257 |
cout << "the matrix with size = " << _dim0 << " is singular with " |
| 258 |
<< n0 << " dimenisonal kernel." |
| 259 |
<< endl; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void Solver(const MatriceMorse<R> &A,KN_<R> &x,const KN_<R> &b) const { |
| 264 |
ffassert (&x[0] != &b[0]); |
| 265 |
int dim = A.n; |
| 266 |
// #x_1[] = _dim0; x_1 = b_1 - A_12 x_2 |
| 267 |
for (int i = 0; i < _dim0; i++) { |
| 268 |
_xtmp[i] = b[_new2old[i]]; |
| 269 |
} |
| 270 |
for (int i = _dim0; i < dim; i++) { |
| 271 |
const int ii = _new2old[i]; |
| 272 |
x[ii] = b[ii] / _tgv; |
| 273 |
} |
| 274 |
for (int i = 0; i < _dim0; i++) { |
| 275 |
for (int k = _ptrows1[i]; k < _ptrows1[i + 1]; k++) { |
| 276 |
_xtmp[i] -= _coefs1[k] * x[_new2old[_indcols1[k]]]; |
| 277 |
} |
| 278 |
} |
| 279 |
const int projection = 1; |
| 280 |
const int transpose = 0; |
| 281 |
diss_solve_1(*_dslv, _xtmp, projection, transpose); |
| 282 |
for (int i = 0; i < _dim0; i++) { |
| 283 |
x[_new2old[i]] = _xtmp[i]; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
~SolveDissection() { |
| 288 |
diss_free(*_dslv); |
| 289 |
delete _dslv; |
| 290 |
delete [] _new2old; |
| 291 |
delete [] _ptrows0; |
| 292 |
delete [] _indcols0; |
| 293 |
delete [] _coefs0; |
| 294 |
delete [] _ptrows1; |
| 295 |
delete [] _indcols1; |
| 296 |
delete [] _coefs1; |
| 297 |
delete [] _xtmp; |
| 298 |
} |
| 299 |
|
| 300 |
void addMatMul(const KN_<R> & x, KN_<R> & Ax) const |
| 301 |
{ |
| 302 |
ffassert(x.N()==Ax.N()); |
| 303 |
Ax += (const MatriceMorse<R> &) (*this) * x; |
| 304 |
} |
| 305 |
|
| 306 |
uint64_t *dslv() { |
| 307 |
return _dslv; |
| 308 |
} |
| 309 |
int dim0() const { return _dim0; } |
| 310 |
int * new2old() const { return _new2old; } |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
template<> |
| 315 |
class SolveDissection<Complex> : public MatriceMorse<Complex>::VirtualSolver { |
| 316 |
// double eps; |
| 317 |
// mutable double epsr; |
| 318 |
double _tgv; |
| 319 |
double _pivot; //, _pivot_sym; |
| 320 |
int _dim0; |
| 321 |
int *_new2old; |
| 322 |
int *_ptrows0; |
| 323 |
int *_indcols0; |
| 324 |
Complex *_coefs0; |
| 325 |
int *_ptrows1; |
| 326 |
int *_indcols1; |
| 327 |
Complex *_coefs1; |
| 328 |
Complex *_xtmp; |
| 329 |
uint64_t *_dslv; |
| 330 |
public: |
| 331 |
SolveDissection(const MatriceMorse<Complex> &A, |
| 332 |
int strategy_, double ttgv, |
| 333 |
double pivot) : |
| 334 |
_tgv(ttgv), |
| 335 |
_pivot(pivot) |
| 336 |
{ |
| 337 |
const int dim = A.n; |
| 338 |
int num_threads = 1; |
| 339 |
if (getenv("DISSECTION_NUM_THREADS")) { |
| 340 |
sscanf(getenv("DISSECTION_NUM_THREADS"), "%d", &num_threads); |
| 341 |
fprintf(stderr, |
| 342 |
"environmental variable DISSECTION_NUM_THREADS = %d\n", |
| 343 |
num_threads); |
| 344 |
} |
| 345 |
|
| 346 |
_dslv = new uint64_t; |
| 347 |
diss_init(*_dslv, 0, 2, num_threads, ((verbosity > 3) ? 1 : 0)); // |
| 348 |
// complex matrix, with double precision factorization, # of threads |
| 349 |
const int decomposer = (strategy_ % 100) / 10; |
| 350 |
const int scaling = strategy_ == 0 ? 2 : strategy_ % 10; |
| 351 |
// sym + lower + isWhole = 1 + 2 + 4 |
| 352 |
const int sym = (strategy_ / 100) ? 5 : 0; |
| 353 |
|
| 354 |
int *old2new = new int[dim]; |
| 355 |
_new2old = new int[dim]; |
| 356 |
{ |
| 357 |
int m = 0; |
| 358 |
for (int i = 0; i < dim; i++) { |
| 359 |
for (int k = (A.lg)[i]; k < (A.lg)[i + 1]; k++) { |
| 360 |
const int j = (A.cl)[k]; |
| 361 |
if (i == j) { |
| 362 |
if ((A.a)[k] != ttgv) { |
| 363 |
_new2old[m] = i; |
| 364 |
m++; |
| 365 |
} |
| 366 |
break; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
_dim0 = m; |
| 371 |
for (int i = 0; i < dim; i++) { |
| 372 |
for (int k = (A.lg)[i]; k < (A.lg)[i + 1]; k++) { |
| 373 |
const int j = (A.cl)[k]; |
| 374 |
if (i == j) { |
| 375 |
if ((A.a)[k] == ttgv) { |
| 376 |
_new2old[m] = i; |
| 377 |
m++; |
| 378 |
} |
| 379 |
break; |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
if (verbosity > 10) { |
| 384 |
cout << "m = " << m << " dim = " << dim << endl; |
| 385 |
} |
| 386 |
} |
| 387 |
for (int i = 0; i < dim; i++) { |
| 388 |
old2new[_new2old[i]] = i; |
| 389 |
} |
| 390 |
// |
| 391 |
_xtmp = new Complex[_dim0]; |
| 392 |
int nnz; |
| 393 |
list<int> *indcols_tmp = new list<int>[_dim0]; |
| 394 |
list<Complex> *coefs_tmp = new list<Complex>[_dim0]; |
| 395 |
nnz = generate_CSR<Complex>(indcols_tmp, coefs_tmp, dim, _dim0, old2new, |
| 396 |
_new2old, |
| 397 |
(int *)A.lg, (int *)A.cl, (Complex *)A.a); |
| 398 |
delete [] old2new; |
| 399 |
if (verbosity > 10) { |
| 400 |
cout << "nnz " << nnz << endl; |
| 401 |
} |
| 402 |
_ptrows0 = new int[_dim0 + 1]; |
| 403 |
_ptrows1 = new int[_dim0 + 1]; |
| 404 |
int *indcol_tmp = new int[nnz]; |
| 405 |
Complex *coef_tmp = new Complex[nnz]; |
| 406 |
int nnz1; |
| 407 |
nnz1 = copy_CSR<Complex>(_ptrows0, indcol_tmp, coef_tmp, _dim0, |
| 408 |
indcols_tmp, coefs_tmp, 0, _dim0); |
| 409 |
if (verbosity > 10) { |
| 410 |
cout << "nnz1 " << nnz1 << endl; |
| 411 |
} |
| 412 |
_indcols0 = new int[nnz1]; |
| 413 |
_coefs0 = new Complex[nnz1]; |
| 414 |
for (int i = 0; i < nnz1; i++) { |
| 415 |
_indcols0[i] = indcol_tmp[i]; |
| 416 |
_coefs0[i] = coef_tmp[i]; |
| 417 |
} |
| 418 |
nnz1 = copy_CSR<Complex>(_ptrows1, indcol_tmp, coef_tmp, _dim0, |
| 419 |
indcols_tmp, coefs_tmp, _dim0, dim); |
| 420 |
if (verbosity > 10) { |
| 421 |
cout << "nnz1 " << nnz1 << endl; |
| 422 |
} |
| 423 |
_indcols1 = new int[nnz1]; |
| 424 |
_coefs1 = new Complex[nnz1]; |
| 425 |
for (int i = 0; i < nnz1; i++) { |
| 426 |
_indcols1[i] = indcol_tmp[i]; |
| 427 |
_coefs1[i] = coef_tmp[i]; |
| 428 |
} |
| 429 |
delete [] indcols_tmp; |
| 430 |
delete [] coefs_tmp; |
| 431 |
delete [] indcol_tmp; |
| 432 |
delete [] coef_tmp; |
| 433 |
diss_s_fact(*_dslv, _dim0, _ptrows0, _indcols0, sym, decomposer); |
| 434 |
const int indefinite_flag = 1; |
| 435 |
const double eps_pivot = _pivot == (-1.0) ? 1.0e-2 : _pivot; |
| 436 |
diss_n_fact(*_dslv, (double *)_coefs0, scaling, eps_pivot, |
| 437 |
indefinite_flag); |
| 438 |
int n0; |
| 439 |
diss_get_kern_dim(*_dslv, &n0); |
| 440 |
if (n0 > 0) { |
| 441 |
cout << "the matrix with size = " << _dim0 << " is singular with " |
| 442 |
<< n0 << " dimenisonal kernel." |
| 443 |
<< endl; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
void Solver(const MatriceMorse<Complex> &A,KN_<Complex> &x,const KN_<Complex> &b) const { |
| 448 |
ffassert (&x[0] != &b[0]); |
| 449 |
int dim = A.n; |
| 450 |
// #x_1[] = _dim0; x_1 = b_1 - A_12 x_2 |
| 451 |
for (int i = 0; i < _dim0; i++) { |
| 452 |
_xtmp[i] = b[_new2old[i]]; |
| 453 |
} |
| 454 |
for (int i = _dim0; i < dim; i++) { |
| 455 |
const int ii = _new2old[i]; |
| 456 |
x[ii] = b[ii] / _tgv; |
| 457 |
} |
| 458 |
for (int i = 0; i < _dim0; i++) { |
| 459 |
for (int k = _ptrows1[i]; k < _ptrows1[i + 1]; k++) { |
| 460 |
_xtmp[i] -= _coefs1[k] * x[_new2old[_indcols1[k]]]; |
| 461 |
} |
| 462 |
} |
| 463 |
const int projection = 1; |
| 464 |
const int transpose = 0; |
| 465 |
diss_solve_1(*_dslv, (double *)_xtmp, projection, transpose); |
| 466 |
for (int i = 0; i < _dim0; i++) { |
| 467 |
x[_new2old[i]] = _xtmp[i]; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
~SolveDissection() { |
| 472 |
diss_free(*_dslv); |
| 473 |
delete _dslv; |
| 474 |
} |
| 475 |
|
| 476 |
void addMatMul(const KN_<Complex> & x, KN_<Complex> & Ax) const |
| 477 |
{ |
| 478 |
ffassert(x.N()==Ax.N()); |
| 479 |
Ax += (const MatriceMorse<Complex> &) (*this) * x; |
| 480 |
} |
| 481 |
|
| 482 |
uint64_t *dslv() { |
| 483 |
return _dslv; |
| 484 |
} |
| 485 |
int dim0() const { return _dim0; } |
| 486 |
int * new2old() const { return _new2old; } |
| 487 |
|
| 488 |
}; |
| 489 |
|
| 490 |
inline MatriceMorse<double>::VirtualSolver * |
| 491 |
BuildSolverIDissection(DCL_ARG_SPARSE_SOLVER(double,A)) |
| 492 |
{ |
| 493 |
if( verbosity>9) |
| 494 |
cout << " BuildSolverDissection<double>" << endl; |
| 495 |
return new SolveDissection<double>(*A, |
| 496 |
ds.strategy,ds.tgv,ds.tol_pivot); |
| 497 |
} |
| 498 |
|
| 499 |
inline MatriceMorse<Complex>::VirtualSolver * |
| 500 |
BuildSolverIDissection(DCL_ARG_SPARSE_SOLVER(Complex,A)) |
| 501 |
{ |
| 502 |
if( verbosity>9) |
| 503 |
cout << " BuildSolverDissection<Complex>" << endl; |
| 504 |
return new SolveDissection<Complex>(*A, |
| 505 |
ds.strategy, |
| 506 |
ds.tgv,ds.tol_pivot); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
// the 2 default sparse solver double and complex |
| 511 |
DefSparseSolver<double>::SparseMatSolver SparseMatSolver_R ; |
| 512 |
DefSparseSolver<Complex>::SparseMatSolver SparseMatSolver_C; |
| 513 |
DefSparseSolverSym<double>::SparseMatSolver SparseMatSolverSym_R ; |
| 514 |
DefSparseSolverSym<Complex>::SparseMatSolver SparseMatSolverSym_C; |
| 515 |
// the default probleme solver |
| 516 |
TypeSolveMat::TSolveMat TypeSolveMatdefaultvalue=TypeSolveMat::defaultvalue; |
| 517 |
|
| 518 |
|
| 519 |
template<class K> |
| 520 |
class dissectionkernel_Op : public E_F0mps { |
| 521 |
public: |
| 522 |
Expression mat; |
| 523 |
static const int n_name_param = 3; |
| 524 |
static basicAC_F0::name_and_type name_param[]; |
| 525 |
Expression nargs[n_name_param]; |
| 526 |
dissectionkernel_Op(const basicAC_F0& args, Expression param1) : mat(param1) { |
| 527 |
args.SetNameParam(n_name_param, name_param, nargs); |
| 528 |
} |
| 529 |
AnyType operator()(Stack stack) const; |
| 530 |
}; |
| 531 |
|
| 532 |
template<class K> |
| 533 |
basicAC_F0::name_and_type dissectionkernel_Op<K>::name_param[] = { |
| 534 |
{"kerneldim", &typeid(long *)}, |
| 535 |
{"kerneln", &typeid(KNM<K>*)}, |
| 536 |
{"kernelt", &typeid(KNM<K>*)} |
| 537 |
}; |
| 538 |
|
| 539 |
template<class K> |
| 540 |
class dissectionkernel : public OneOperator { |
| 541 |
public: |
| 542 |
dissectionkernel() : OneOperator(atype<long>(), atype<Matrice_Creuse<K>*>()) {} |
| 543 |
|
| 544 |
E_F0* code(const basicAC_F0& args) const { |
| 545 |
return new dissectionkernel_Op<K>(args, t[0]->CastTo(args[0])); |
| 546 |
} |
| 547 |
}; |
| 548 |
|
| 549 |
#if 0 |
| 550 |
template<class K> |
| 551 |
AnyType dissectionkernel_Op<K>::operator()(Stack stack) const { |
| 552 |
MatriceMorse<K>* mA = static_cast<MatriceMorse<K>*>(&(*GetAny<Matrice_Creuse<K>*>((*mat)(stack))->A)); |
| 553 |
// SolveDissection<K>* mdissection = dynamic_cast<SolveDissection<K> *>(mA); |
| 554 |
const SolveDissection<K>* mdissection; |
| 555 |
mA->GetSolver(mdissection); |
| 556 |
if (mdissection) { |
| 557 |
uint64_t *dslv = ((SolveDissection<K>*)mdissection)->dslv(); |
| 558 |
int n0; |
| 559 |
long *kerndim = nargs[0] ? GetAny<long*>((*nargs[0])(stack)) : 0; |
| 560 |
if (kerndim) { |
| 561 |
diss_get_kern_dim(*dslv, &n0); |
| 562 |
*kerndim = n0; |
| 563 |
} |
| 564 |
KNM<K>* ptKerN = nargs[1] ? GetAny<KNM<K>*>((*nargs[1])(stack)) : 0; |
| 565 |
if(ptKerN) { |
| 566 |
diss_get_kern_dim(*dslv, &n0); |
| 567 |
if (n0 > 0) { |
| 568 |
const int dim = mA->n; |
| 569 |
const int dim0 = mdissection->dim0(); |
| 570 |
const int *new2old = mdissection->new2old(); |
| 571 |
ptKerN->resize(mA->n, n0); |
| 572 |
double *kernv = new double[n0 * dim]; |
| 573 |
diss_get_kern_vecs(*dslv, kernv); |
| 574 |
for (int k = 0; k < n0; k++) { |
| 575 |
for (int i = 0; i < dim0; i++) { |
| 576 |
(*ptKerN)(new2old[i], k) = kernv[i + k * dim0]; |
| 577 |
} |
| 578 |
for (int i = dim0; i < dim; i++) { |
| 579 |
(*ptKerN)(new2old[i], k) = 0.0; |
| 580 |
} |
| 581 |
} |
| 582 |
delete [] kernv; |
| 583 |
} // if (n0 > 0) |
| 584 |
} |
| 585 |
KNM<K>* ptKerT = nargs[2] ? GetAny<KNM<K>*>((*nargs[2])(stack)) : 0; |
| 586 |
if(ptKerT) { |
| 587 |
const int dim = mA->n; |
| 588 |
const int dim0 = mdissection->dim0(); |
| 589 |
const int *new2old = mdissection->new2old(); |
| 590 |
ptKerT->resize(mA->n, n0); |
| 591 |
double *kernv = new double[n0 * dim]; |
| 592 |
diss_get_kernt_vecs(*dslv, kernv); |
| 593 |
for (int k = 0; k < n0; k++) { |
| 594 |
for (int i = 0; i < dim0; i++) { |
| 595 |
(*ptKerT)(new2old[i], k) = kernv[i + k * dim0]; |
| 596 |
} |
| 597 |
for (int i = dim0; i < dim; i++) { |
| 598 |
(*ptKerT)(new2old[i], k) = 0.0; |
| 599 |
} |
| 600 |
} |
| 601 |
delete [] kernv; |
| 602 |
} |
| 603 |
return static_cast<long>(n0); |
| 604 |
} // |
| 605 |
else { |
| 606 |
// error mddisection is not casted! |
| 607 |
return 0L; |
| 608 |
} |
| 609 |
} |
| 610 |
#endif |
| 611 |
// -- |
| 612 |
bool SetDissection() |
| 613 |
{ |
| 614 |
if(verbosity>1) |
| 615 |
cout << " SetDefault sparse solver to Dissection" << endl; |
| 616 |
DefSparseSolver<double>::solver =BuildSolverIDissection; |
| 617 |
DefSparseSolver<Complex>::solver =BuildSolverIDissection; |
| 618 |
DefSparseSolverSym<double>::solver =BuildSolverIDissection; |
| 619 |
DefSparseSolverSym<Complex>::solver =BuildSolverIDissection; |
| 620 |
TypeSolveMat::defaultvalue =TypeSolveMatdefaultvalue; |
| 621 |
return true; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
void init22() |
| 626 |
{ |
| 627 |
SparseMatSolver_R= DefSparseSolver<double>::solver; |
| 628 |
SparseMatSolver_C= DefSparseSolver<Complex>::solver; |
| 629 |
SparseMatSolverSym_R= DefSparseSolverSym<double>::solver; |
| 630 |
SparseMatSolverSym_C= DefSparseSolverSym<Complex>::solver; |
| 631 |
|
| 632 |
if(verbosity>1) |
| 633 |
cout << "\n Add: Dissection: defaultsolver defaultsolverDissection" << endl; |
| 634 |
TypeSolveMat::defaultvalue=TypeSolveMat::SparseSolver; |
| 635 |
DefSparseSolver<double>::solver =BuildSolverIDissection; |
| 636 |
DefSparseSolver<Complex>::solver =BuildSolverIDissection; |
| 637 |
|
| 638 |
if(! Global.Find("defaulttoDissection").NotNull() ) |
| 639 |
Global.Add("defaulttoDissection","(",new OneOperator0<bool>(SetDissection)); |
| 640 |
#if 0 |
| 641 |
Global.Add("dissectionkernel", "(", new dissectionkernel<double>); |
| 642 |
Global.Add("dissectionkernel", "(", new dissectionkernel<Complex>); |
| 643 |
#endif |
| 644 |
} |
| 645 |
|
| 646 |
LOADFUNC(init22); |