| 1 |
/*! \file C_DFullLDLt.cpp |
| 2 |
\brief block factorization routines LDL^t, LDU |
| 3 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 4 |
\date Apr. 22th 2013 |
| 5 |
\date Jul. 12th 2015 |
| 6 |
\date Nov. 30th 2016 |
| 7 |
*/ |
| 8 |
|
| 9 |
// This file is part of Dissection |
| 10 |
// |
| 11 |
// Dissection is free software: you can redistribute it and/or modify |
| 12 |
// it under the terms of the GNU General Public License as published by |
| 13 |
// the Free Software Foundation, either version 3 of the License, or |
| 14 |
// (at your option) any later version. |
| 15 |
// |
| 16 |
// Linking Dissection statically or dynamically with other modules is making |
| 17 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 18 |
// the GNU General Public License cover the whole combination. |
| 19 |
// |
| 20 |
// As a special exception, the copyright holders of Dissection give you |
| 21 |
// permission to combine Dissection program with free software programs or |
| 22 |
// libraries that are released under the GNU LGPL and with independent modules |
| 23 |
// that communicate with Dissection solely through the Dissection-fortran |
| 24 |
// interface. You may copy and distribute such a system following the terms of |
| 25 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 26 |
// provided that you include the source code of that other code when and as |
| 27 |
// the GNU GPL requires distribution of source code and provided that you do |
| 28 |
// not modify the Dissection-fortran interface. |
| 29 |
// |
| 30 |
// Note that people who make modified versions of Dissection are not obligated |
| 31 |
// to grant this special exception for their modified versions; it is their |
| 32 |
// choice whether to do so. The GNU General Public License gives permission to |
| 33 |
// release a modified version without this exception; this exception also makes |
| 34 |
// it possible to release a modified version which carries forward this |
| 35 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 36 |
// does not apply to your modified version of Dissection, and you must remove |
| 37 |
// this exception when you distribute your modified version. |
| 38 |
// |
| 39 |
// This exception is an additional permission under section 7 of the GNU |
| 40 |
// General Public License, version 3 ("GPLv3") |
| 41 |
// |
| 42 |
// Dissection is distributed in the hope that it will be useful, |
| 43 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 |
// GNU General Public License for more details. |
| 46 |
// |
| 47 |
// You should have received a copy of the GNU General Public License |
| 48 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 49 |
// |
| 50 |
|
| 51 |
#include "Driver/C_threads_tasks.hpp" |
| 52 |
#include "Driver/C_KernDetect.hpp" |
| 53 |
#include "Driver/C_BlasRoutines.hpp" |
| 54 |
#include "Driver/DissectionDefault.hpp" |
| 55 |
#include "Compiler/arithmetic.hpp" |
| 56 |
#include "Algebra/VectorArray.hpp" |
| 57 |
#include "Compiler/DissectionIO.hpp" |
| 58 |
#include <time.h> |
| 59 |
|
| 60 |
#ifdef BLAS_MKL |
| 61 |
#define MKL_DOMATCOPY |
| 62 |
#endif |
| 63 |
|
| 64 |
template<typename T> |
| 65 |
void C_dupdateb_Schur_diag(void *arg_) |
| 66 |
{ |
| 67 |
C_dupdateb_Schur_arg<T> *arg = (C_dupdateb_Schur_arg<T> *)arg_; |
| 68 |
const int task_position = arg->task_position; |
| 69 |
const int id_block = arg->id_block; |
| 70 |
const int id_level = arg->id_level; |
| 71 |
const int n = arg->n; |
| 72 |
const int nrow = arg->nrow; // size of diag. block whose matrix is factorized |
| 73 |
const int ncol = arg->ncol; // |
| 74 |
const int i1_block = arg->i1_block; |
| 75 |
const int ii_block = arg->ii_block; |
| 76 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 77 |
const int n0 = D.dim_kern_block(id_block); |
| 78 |
|
| 79 |
const T none(-1.0); |
| 80 |
const T one(1.0); |
| 81 |
|
| 82 |
if ((task_position % 2 == 1) && (id_level == 0)) { |
| 83 |
T *a = arg->a->addrCoefs(); //T *a = *(arg->a); |
| 84 |
D.copyBlockToArray(arg->ii_block, arg->ii_block, a, n); |
| 85 |
} |
| 86 |
|
| 87 |
if (arg->isSym) { |
| 88 |
// alpha=-1 |
| 89 |
// beta=1 |
| 90 |
C_gemm_symm<T>(ncol, |
| 91 |
(nrow - n0), // nrow : zero padding is better ? |
| 92 |
none, // alpha |
| 93 |
D.addrCoefBlock(ii_block, i1_block), // lower transposed |
| 94 |
D.nrowBlock(ii_block, i1_block), |
| 95 |
D.addrCoefBlock(i1_block, ii_block), // upper |
| 96 |
D.nrowBlock(i1_block, ii_block), |
| 97 |
one, // beta, |
| 98 |
D.addrCoefBlock(ii_block, ii_block), // Schur |
| 99 |
D.nrowBlock(ii_block, ii_block)); |
| 100 |
} |
| 101 |
else { |
| 102 |
blas_gemm<T>(CblasTrans, CblasNoTrans, |
| 103 |
ncol, |
| 104 |
ncol, |
| 105 |
(nrow - n0), // nrow : zero padding is better ? |
| 106 |
none, // alpha, |
| 107 |
D.addrCoefBlock(ii_block, i1_block), // lower transposed |
| 108 |
D.nrowBlock(ii_block, i1_block), |
| 109 |
D.addrCoefBlock(i1_block, ii_block), // upper |
| 110 |
D.nrowBlock(i1_block, ii_block), |
| 111 |
one, // beta, |
| 112 |
D.addrCoefBlock(ii_block, ii_block), // Schur |
| 113 |
D.nrowBlock(ii_block, ii_block)); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
template |
| 118 |
void C_dupdateb_Schur_diag<double>(void *arg_); |
| 119 |
|
| 120 |
template |
| 121 |
void C_dupdateb_Schur_diag<quadruple>(void *arg_); |
| 122 |
|
| 123 |
template |
| 124 |
void C_dupdateb_Schur_diag<complex<double> >(void *arg_); |
| 125 |
|
| 126 |
template |
| 127 |
void C_dupdateb_Schur_diag<complex<quadruple> >(void *arg_); |
| 128 |
|
| 129 |
template |
| 130 |
void C_dupdateb_Schur_diag<float>(void *arg_); |
| 131 |
|
| 132 |
template |
| 133 |
void C_dupdateb_Schur_diag<complex<float> >(void *arg_); |
| 134 |
// |
| 135 |
|
| 136 |
template<typename T> |
| 137 |
void C_dupdateb_Schur_offdiag(void *arg_) |
| 138 |
{ |
| 139 |
C_dupdateb_Schur_arg<T> *arg = (C_dupdateb_Schur_arg<T> *)arg_; |
| 140 |
const int task_position = arg->task_position; |
| 141 |
const int id_block = arg->id_block; |
| 142 |
const int id_level = arg->id_level; |
| 143 |
const int n = arg->n; |
| 144 |
const int nrow = arg->nrow; |
| 145 |
const int ncol = arg->ncol; |
| 146 |
const int i1_block = arg->i1_block; |
| 147 |
const int ii_block = arg->ii_block; |
| 148 |
const int jj_block = arg->jj_block; |
| 149 |
|
| 150 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 151 |
const int n0 = D.dim_kern_block(id_block); |
| 152 |
|
| 153 |
const T none(-1.0); |
| 154 |
const T one(1.0); |
| 155 |
|
| 156 |
if ((task_position % 2 == 1) && (id_level == 0)) { |
| 157 |
T *a = arg->a->addrCoefs(); //T *a = *(arg->a); |
| 158 |
D.copyBlockToArray(arg->ii_block, arg->jj_block, a, n); |
| 159 |
if (!arg->isSym) { |
| 160 |
D.copyBlockToArray(arg->jj_block, arg->ii_block, a, n); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
blas_gemm<T>(CblasTrans, CblasNoTrans, |
| 165 |
nrow, |
| 166 |
ncol, |
| 167 |
(nrow - n0), // |
| 168 |
none, // alpha, |
| 169 |
D.addrCoefBlock(ii_block, i1_block), // lower |
| 170 |
D.nrowBlock(ii_block, i1_block), |
| 171 |
D.addrCoefBlock(i1_block, jj_block), // upper |
| 172 |
D.nrowBlock(i1_block, jj_block), |
| 173 |
one, // beta, |
| 174 |
D.addrCoefBlock(ii_block, jj_block), // Schur |
| 175 |
D.nrowBlock(ii_block, jj_block)); |
| 176 |
if (!arg->isSym) { |
| 177 |
// S(jj,ii)-=L(jj)*U(ii) <==> S(jj,ii)^T-=L(jj)^T*U(ii)^T |
| 178 |
blas_gemm<T>(CblasTrans, CblasNoTrans, |
| 179 |
nrow, // row of S^T |
| 180 |
ncol, |
| 181 |
(nrow - n0), // |
| 182 |
none, // alpha, |
| 183 |
D.addrCoefBlock(i1_block, ii_block), // upper^T |
| 184 |
D.nrowBlock(i1_block, ii_block), |
| 185 |
D.addrCoefBlock(jj_block, i1_block), // lower^T |
| 186 |
D.nrowBlock(jj_block, i1_block), |
| 187 |
one, // beta, |
| 188 |
D.addrCoefBlock(jj_block, ii_block), // Schur |
| 189 |
D.nrowBlock(jj_block, ii_block)); |
| 190 |
} |
| 191 |
} |
| 192 |
template |
| 193 |
void C_dupdateb_Schur_offdiag<double>(void *arg_); |
| 194 |
|
| 195 |
template |
| 196 |
void C_dupdateb_Schur_offdiag<quadruple>(void *arg_); |
| 197 |
|
| 198 |
template |
| 199 |
void C_dupdateb_Schur_offdiag<complex<double> >(void *arg_); |
| 200 |
|
| 201 |
template |
| 202 |
void C_dupdateb_Schur_offdiag<complex<quadruple> >(void *arg_); |
| 203 |
|
| 204 |
template |
| 205 |
void C_dupdateb_Schur_offdiag<float>(void *arg_); |
| 206 |
|
| 207 |
template |
| 208 |
void C_dupdateb_Schur_offdiag<complex<float> >(void *arg_); |
| 209 |
// |
| 210 |
|
| 211 |
template<typename T, typename U> |
| 212 |
void C_dfull_gauss_b(void *arg_) |
| 213 |
{ |
| 214 |
const T zero(0.0); |
| 215 |
C_dfull_gauss_arg<T, U> *arg = (C_dfull_gauss_arg<T, U> *)arg_; |
| 216 |
const int task_position = arg->task_position; |
| 217 |
const int id_block = arg->id_block; |
| 218 |
const int id_level = arg->id_level; |
| 219 |
const int n = arg->n; |
| 220 |
const bool verbose = arg->verbose; |
| 221 |
FILE *fp = *(arg->fp); |
| 222 |
|
| 223 |
// int *nnn0 = arg->n0; |
| 224 |
int nn0; |
| 225 |
const int nrow = arg->nrow; |
| 226 |
const int i1_block = arg->i1_block; |
| 227 |
|
| 228 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 229 |
const int i1 = D.IndexBlock(i1_block); |
| 230 |
|
| 231 |
int *permute_block = new int[D.nrowBlock(i1_block)]; // block_size()] ; |
| 232 |
const int aug_dim = *(arg->aug_dim); |
| 233 |
const U eps_machine = *(arg->eps_machine); |
| 234 |
const double eps_piv = *(arg->eps_piv); |
| 235 |
|
| 236 |
// get smaller pivot of two children |
| 237 |
|
| 238 |
T *a_diag = D.addrCoefBlock(arg->i1_block, arg->i1_block); |
| 239 |
int nrow_block = D.nrowBlock(arg->i1_block, arg->i1_block); |
| 240 |
vector<int>& permute = D.getPermute(); |
| 241 |
double pivot, pivot0, pivot1; |
| 242 |
vector<int>& singIdx0 = D.getSingIdx0(); |
| 243 |
|
| 244 |
if (task_position % 2 == 1) { |
| 245 |
// copy the whole matrix for kernel detection and refactorization |
| 246 |
// |
| 247 |
if (*(arg->kernel_detection) || (id_level == 0)) { // 30 Jul.2014 |
| 248 |
ColumnMatrix<T> &a = *(arg->a); |
| 249 |
a.init(n, n); |
| 250 |
// symmetrizie the first diagonal block : copy upper to lower |
| 251 |
if (arg->isSym) { |
| 252 |
for (int i = 0; i < nrow_block; i++) { |
| 253 |
for (int j = 0; j < i; j++) { // access lower |
| 254 |
a_diag[i + j * nrow_block] = a_diag[j + i * nrow_block]; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
D.copyBlockToArray(arg->i1_block, arg->i1_block, a.addrCoefs(), n); |
| 259 |
// D.copyToArrayFull(a, n); |
| 260 |
} // if (id_level == 0) |
| 261 |
singIdx0.clear(); |
| 262 |
singIdx0.reserve(n); |
| 263 |
// singIdx0.resize(n, -1); |
| 264 |
D.set_dim_kern(0); // counting total number of null pivots among blocks |
| 265 |
// initialize permute[] |
| 266 |
for (int i = 0; i < n; i++) { |
| 267 |
permute[i] = i; |
| 268 |
} |
| 269 |
pivot0 = *(arg->pivot0); |
| 270 |
pivot1 = *(arg->pivot1); |
| 271 |
pivot = (pivot0 < pivot1 ? pivot0 : pivot1); |
| 272 |
*(arg->quit) = false; // default is to continue queue for refactorization |
| 273 |
} // if (task_position % 2 == 1) |
| 274 |
else { |
| 275 |
pivot = *(arg->pivot); |
| 276 |
} |
| 277 |
|
| 278 |
const int n0 = 0; |
| 279 |
double fop; |
| 280 |
|
| 281 |
if ((task_position % 2 == 1) && (nrow > aug_dim)) { |
| 282 |
bool flag_repeat_piv = true; |
| 283 |
// bool repeat_first = true; |
| 284 |
double eps_piv1 = eps_piv; |
| 285 |
int count_repeat = 0; |
| 286 |
ColumnMatrix<T> a_diag1(nrow_block, nrow_block); |
| 287 |
while (flag_repeat_piv) { |
| 288 |
if (count_repeat == 0) { |
| 289 |
blas_copy<T>((nrow_block * nrow_block), |
| 290 |
a_diag, 1, a_diag1.addrCoefs(), 1); |
| 291 |
} |
| 292 |
else { |
| 293 |
blas_copy<T>((nrow_block * nrow_block), |
| 294 |
a_diag1.addrCoefs(), 1, a_diag, 1); |
| 295 |
} // if (count_repeat > 0) |
| 296 |
if (arg->isSym) { |
| 297 |
full_ldlt_permute<T, U>(&nn0, n0, nrow, a_diag, nrow_block, &pivot, |
| 298 |
permute_block, |
| 299 |
eps_piv1, &fop); |
| 300 |
} |
| 301 |
else { |
| 302 |
full_ldu_permute<T, U>(&nn0, n0, nrow, a_diag, nrow_block, &pivot, |
| 303 |
permute_block, |
| 304 |
eps_piv1, &fop); |
| 305 |
} // if (arg->isSym) |
| 306 |
if (((nrow - nn0) >= aug_dim) || (eps_piv1 < TOL_PIVOT)) { |
| 307 |
flag_repeat_piv = false; |
| 308 |
} |
| 309 |
else { |
| 310 |
eps_piv1 /= 10.0; |
| 311 |
count_repeat++; |
| 312 |
} |
| 313 |
} // while (flag_repeat_piv) |
| 314 |
if (eps_piv1 < TOL_PIVOT) { |
| 315 |
D.set_pivrelaxed(); |
| 316 |
} |
| 317 |
a_diag1.free(); |
| 318 |
if (count_repeat > 0) { |
| 319 |
diss_printf(verbose, fp, |
| 320 |
"%s %d : eps_piv = %g pivot = %g n0 = %d count_repeat = %d\n", |
| 321 |
__FILE__, __LINE__, eps_piv1, pivot, n0, count_repeat); |
| 322 |
} |
| 323 |
} // if ((id_level == 0) && (task_position % 2 == 1)) |
| 324 |
else { |
| 325 |
if (arg->isSym) { |
| 326 |
full_ldlt_permute<T, U>(&nn0, n0, nrow, a_diag, nrow_block, &pivot, |
| 327 |
permute_block, |
| 328 |
eps_piv, &fop); |
| 329 |
} // if (arg->isSym) |
| 330 |
else { |
| 331 |
full_ldu_permute<T, U>(&nn0, n0, nrow, a_diag, nrow_block, &pivot, |
| 332 |
permute_block, |
| 333 |
eps_piv, &fop); |
| 334 |
} |
| 335 |
} |
| 336 |
if (nn0 > 0) { |
| 337 |
diss_printf(verbose, fp, |
| 338 |
"%s %d : nd = %d : level = %d block = %d null = %d / %d\n", |
| 339 |
__FILE__, __LINE__, arg->nb, arg->id_level, arg->id_block, |
| 340 |
nn0, nrow); |
| 341 |
} |
| 342 |
// permute_block is defined by Fortran array, i.e. takes index starting 1 |
| 343 |
for (int i = i1; i < i1 + nrow; i++) { |
| 344 |
permute[i] = permute_block[i - i1] + i1; |
| 345 |
} |
| 346 |
// store singular nodes |
| 347 |
int sing_max = D.dim_kern(); // D.sing_max(); |
| 348 |
{ |
| 349 |
int itmp = i1 + nrow - nn0; |
| 350 |
for (int i = 0; i < nn0; i++, itmp++, sing_max++) { |
| 351 |
// singIdx0[sing_max] = itmp; |
| 352 |
singIdx0.push_back(itmp); |
| 353 |
} |
| 354 |
D.set_dim_kern(sing_max); // total number of null pivots |
| 355 |
D.set_dim_kern_block(id_block, nn0); // #null pivots candidate in the block |
| 356 |
} |
| 357 |
|
| 358 |
if (task_position / 2 == 1) { |
| 359 |
{ // scope for list<int> tmp_idx |
| 360 |
list<int> tmp_idx; |
| 361 |
for (int i = 0; i < sing_max; i++) { |
| 362 |
for (int j = -1; j <= 1; j++) { |
| 363 |
const int itmp = singIdx0[i] + j; |
| 364 |
if (itmp >= 0 && itmp < n) { |
| 365 |
tmp_idx.push_back(itmp); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
tmp_idx.sort(); |
| 370 |
tmp_idx.unique(); |
| 371 |
|
| 372 |
if (sing_max > 0) { |
| 373 |
diss_printf(verbose, fp, |
| 374 |
"%s %d : %d : %g\n", __FILE__, __LINE__, arg->nb, pivot); |
| 375 |
} |
| 376 |
for (list<int>::const_iterator it = tmp_idx.begin(); |
| 377 |
it != tmp_idx.end(); ++it) { |
| 378 |
bool dispflag = false; |
| 379 |
for (int j = 0; j < sing_max; j++) { |
| 380 |
if ((*it) == singIdx0[j]) { |
| 381 |
dispflag = true; |
| 382 |
} |
| 383 |
} |
| 384 |
diss_printf(verbose, fp, "%d %s %s\n", (*it), (dispflag ? "*" :":"), |
| 385 |
tostring<T>(D.diag(*it)).c_str()); |
| 386 |
} |
| 387 |
} |
| 388 |
// nullifying diagonal entries |
| 389 |
for (int m = 0; m < sing_max; m++) { |
| 390 |
const int ii = singIdx0[m]; |
| 391 |
D.diag(ii) = zero; |
| 392 |
} |
| 393 |
bool refactorize = false; // default value |
| 394 |
// check dim of factorized matrix |
| 395 |
int dim_kern = 0; |
| 396 |
bool flagKernelDetect = true; |
| 397 |
if (sing_max > 0) { |
| 398 |
diss_printf(verbose, fp, |
| 399 |
"%s %d : C_dfull_gauss_b : id_level %d : nb = %d : %d / %d ", |
| 400 |
__FILE__, __LINE__, |
| 401 |
id_level, arg->nb, arg->id_block, arg->num_block); |
| 402 |
diss_printf(verbose, fp, "task_position = %d, i1 = %d , nrow = %d\n", |
| 403 |
task_position, i1, nrow); |
| 404 |
diss_printf(verbose, fp, "%s %d : sing_max = %d : ", |
| 405 |
__FILE__, __LINE__, sing_max); |
| 406 |
for (int i = 0; i < sing_max; i++) { |
| 407 |
diss_printf(verbose, fp, "%d ", singIdx0[i]); |
| 408 |
} |
| 409 |
diss_printf(verbose, fp, "\n"); |
| 410 |
|
| 411 |
if ((task_position == 3) && (sing_max + aug_dim) > nrow) { |
| 412 |
// if (task_position == 3) { |
| 413 |
*(arg->quit) = true; // refactorize is only applied for the root matrix |
| 414 |
diss_printf(verbose, fp, |
| 415 |
"%s %d : nonsingular part is too small %d : %d - %d : %d\n", |
| 416 |
__FILE__, __LINE__, |
| 417 |
aug_dim, nrow, nn0, sing_max); |
| 418 |
flagKernelDetect = false; |
| 419 |
dim_kern = sing_max; // nullify rows with all suspicious pivots |
| 420 |
D.set_KernelDetected(false); // kernel is unknown |
| 421 |
singIdx0.resize(sing_max); |
| 422 |
} |
| 423 |
} // if (sing_max > 0) |
| 424 |
else { |
| 425 |
flagKernelDetect = false; |
| 426 |
singIdx0.resize(0); |
| 427 |
dim_kern = 0; |
| 428 |
D.set_KernelDetected(true); |
| 429 |
} |
| 430 |
if (flagKernelDetect) { |
| 431 |
if (*(arg->kernel_detection) || (id_level == 0)) { |
| 432 |
singIdx0.resize(sing_max); |
| 433 |
dim_kern = dimKernDense<T, U>(singIdx0, n, |
| 434 |
aug_dim, |
| 435 |
eps_machine, |
| 436 |
eps_piv, |
| 437 |
D, |
| 438 |
arg->a->addrCoefs(), //*(arg->a), |
| 439 |
refactorize, |
| 440 |
false, // isFullPermute |
| 441 |
arg->isSym, |
| 442 |
verbose, |
| 443 |
fp); |
| 444 |
diss_printf(verbose, fp, "%s %d : dim_kern = %d : singIdx0 = [", |
| 445 |
__FILE__, __LINE__, dim_kern); |
| 446 |
for (int i = 0; i < singIdx0.size(); i++) { |
| 447 |
diss_printf(verbose, fp, "%d ", singIdx0[i]); |
| 448 |
} |
| 449 |
diss_printf(verbose, fp, "]\n"); |
| 450 |
if (dim_kern == (-1)) { |
| 451 |
// refactorization happens in only the last level |
| 452 |
refactorize = true; |
| 453 |
D.set_KernelDetected(false); |
| 454 |
} |
| 455 |
else if (dim_kern != singIdx0.size()) { // 20 Feb.2017 |
| 456 |
dim_kern = sing_max; // nullify rows with all suspicious pivots |
| 457 |
D.set_KernelDetected(false); // kernel is unknown |
| 458 |
singIdx0.resize(sing_max); |
| 459 |
} |
| 460 |
else { |
| 461 |
D.set_KernelDetected(true); |
| 462 |
} |
| 463 |
} //else if (*(arg->kernel_detection) || (id_level == 0)) { |
| 464 |
else { |
| 465 |
diss_printf(verbose, fp, |
| 466 |
"%s %d : kernel detection will be done in the last\n", |
| 467 |
__FILE__, __LINE__); |
| 468 |
dim_kern = sing_max; // nullify rows with all suspicious pivots |
| 469 |
D.set_KernelDetected(false); // kernel is unknown |
| 470 |
singIdx0.resize(sing_max); |
| 471 |
} //else if (*(arg->kernel_detection) || (id_level == 0)) { |
| 472 |
} // if (flagKernelDetect) |
| 473 |
// if (sing_max > 0) |
| 474 |
if(arg->isSym) { |
| 475 |
D.freeLowerBlocks(); |
| 476 |
} |
| 477 |
if (refactorize){ |
| 478 |
//reset permutation for refactorization |
| 479 |
for (int i = 0; i < n; i++) { |
| 480 |
permute[i] = i; |
| 481 |
} |
| 482 |
diss_printf(verbose, fp, |
| 483 |
"%s %d : refactorization with whole diagonal pivots starts\n", |
| 484 |
__FILE__, __LINE__); |
| 485 |
*(arg->quit) = false; |
| 486 |
} |
| 487 |
else { // if (!refactorize) |
| 488 |
if (*(arg->kernel_detection) || (id_level == 0)) { // 30 Jul.2014 |
| 489 |
arg->a->free(); |
| 490 |
} |
| 491 |
if(id_level == 0) { |
| 492 |
*(arg->quit) = true; // refactorize is only applied for the root matrix |
| 493 |
} |
| 494 |
D.getSingIdx().resize(sing_max); |
| 495 |
for (int i = 0; i < sing_max; i++) { |
| 496 |
D.getSingIdx()[i] = permute[singIdx0[i]]; |
| 497 |
} |
| 498 |
D.set_lastPivot(pivot); |
| 499 |
D.set_dim_kern(dim_kern); |
| 500 |
D.set_rank(n - dim_kern); // |
| 501 |
} // if (refactorize) |
| 502 |
} // if (task_position / 2 == 1) |
| 503 |
else { |
| 504 |
if (nn0 > 0) { |
| 505 |
// nullifying diangol entries |
| 506 |
for (int ii = (i1 + nrow - nn0); ii < (i1 + nrow); ii++) { |
| 507 |
D.diag(ii) = zero; // a[ii * (n + 1)] = 0.0; |
| 508 |
} |
| 509 |
} // if (nn0 > 0) |
| 510 |
} // if (task_position / 2 == 1) |
| 511 |
*(arg->pivot) = pivot; // for passing information to other task |
| 512 |
delete [] permute_block; |
| 513 |
} |
| 514 |
|
| 515 |
template |
| 516 |
void C_dfull_gauss_b<double, double>(void *arg_); |
| 517 |
|
| 518 |
template |
| 519 |
void C_dfull_gauss_b<complex<double>, double>(void *arg_); |
| 520 |
|
| 521 |
template |
| 522 |
void C_dfull_gauss_b<quadruple, quadruple>(void *arg_); |
| 523 |
|
| 524 |
template |
| 525 |
void C_dfull_gauss_b<complex<quadruple>, quadruple>(void *arg_); |
| 526 |
|
| 527 |
template |
| 528 |
void C_dfull_gauss_b<float, float>(void *arg_); |
| 529 |
|
| 530 |
template |
| 531 |
void C_dfull_gauss_b<complex<float>, float>(void *arg_); |
| 532 |
|
| 533 |
// |
| 534 |
|
| 535 |
template<typename T> |
| 536 |
void C_dinvDL_timesU(void *arg_) |
| 537 |
{ |
| 538 |
const T one(1.0); |
| 539 |
C_dinvDL_timesU_arg<T> *arg = (C_dinvDL_timesU_arg<T> *)arg_; |
| 540 |
const int task_position = arg->task_position; |
| 541 |
const int id_level = arg->id_level; |
| 542 |
const int id_block = arg->id_block; |
| 543 |
const int n = arg->n; |
| 544 |
const int nrow = arg->nrow; |
| 545 |
const int ncol = arg->ncol; |
| 546 |
|
| 547 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 548 |
const int i1 = D.IndexBlock(arg->i1_block); |
| 549 |
|
| 550 |
T *a_diag = D.addrCoefBlock(arg->i1_block, arg->i1_block); |
| 551 |
T *a_upper = D.addrCoefBlock(arg->i1_block, arg->jj_block); |
| 552 |
const int nrow_block_diag = D.nrowBlock(arg->i1_block, arg->i1_block); |
| 553 |
const int nrow_block_upper= D.nrowBlock(arg->i1_block, arg->jj_block); |
| 554 |
if (arg->isSym) { |
| 555 |
D.allocateBlock(arg->jj_block, arg->i1_block); |
| 556 |
} |
| 557 |
|
| 558 |
T *a_lower = D.addrCoefBlock(arg->jj_block, arg->i1_block); |
| 559 |
const int nrow_block_lower = D.nrowBlock(arg->jj_block, arg->i1_block); |
| 560 |
|
| 561 |
vector<int>& permute = D.getPermute(); |
| 562 |
const int n0 = D.dim_kern_block(id_block); |
| 563 |
// assuming to be allocated in the cache memory |
| 564 |
VectorArray<T> a_tmp(nrow); |
| 565 |
if ((task_position % 2 == 1) && (id_level == 0)) { |
| 566 |
// T *a = *(arg->a); |
| 567 |
ColumnMatrix<T> &a = *(arg->a); |
| 568 |
// copy a_upper |
| 569 |
D.copyBlockToArray(arg->i1_block, arg->jj_block, a.addrCoefs(), n); |
| 570 |
if (!arg->isSym) { |
| 571 |
// copy a_lower |
| 572 |
D.copyBlockToArray(arg->jj_block, arg->i1_block, a.addrCoefs(), n); |
| 573 |
} |
| 574 |
} |
| 575 |
{ |
| 576 |
// copy upper to upper with permutation |
| 577 |
for (int j = 0; j < ncol; j++) { |
| 578 |
const int jn = j * nrow; |
| 579 |
for (int i = 0; i < nrow; i++) { |
| 580 |
const int ip = permute[i1 + i] - i1; |
| 581 |
a_tmp[i] = a_upper[ip + jn]; |
| 582 |
} |
| 583 |
blas_copy<T>(nrow, a_tmp.addrCoefs(), 1, a_upper + jn, 1); |
| 584 |
} |
| 585 |
} |
| 586 |
if (!arg->isSym) { |
| 587 |
// copy transposed lower to transposed lower with permutation |
| 588 |
for (int j = 0; j < ncol; j++) { |
| 589 |
const int jn = j * nrow; |
| 590 |
for (int i = 0; i < nrow; i++) { |
| 591 |
const int ip = permute[i1 + i] - i1; |
| 592 |
a_tmp[i] = a_lower[ip + jn]; |
| 593 |
} |
| 594 |
blas_copy<T>(nrow, a_tmp.addrCoefs(), 1, a_lower + jn, 1); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
blas_trsm<T>(CblasLeft, CblasLower, CblasNoTrans, CblasUnit, |
| 599 |
(nrow - n0), // n0 : dim of suspicious pivots |
| 600 |
ncol, |
| 601 |
one, // alpha, |
| 602 |
a_diag, |
| 603 |
nrow_block_diag, // 3 Dec. 2015 |
| 604 |
a_upper, |
| 605 |
nrow_block_upper); // Dec. 2015 |
| 606 |
if (arg->isSym) { |
| 607 |
for (int j = 0; j < ncol; j++) { |
| 608 |
const int jn = j * nrow; |
| 609 |
blas_copy<T>(nrow, a_upper + jn, 1, a_lower + jn, 1); |
| 610 |
} |
| 611 |
} |
| 612 |
else { |
| 613 |
// (A_{jj i1} U_{i1 i1}^-1)^T = U_{i1 i1}^{-T} A_{jj i1}^{T} |
| 614 |
blas_trsm<T>(CblasLeft, CblasUpper, CblasTrans, CblasUnit, |
| 615 |
(nrow - n0), // n0 : dim of suspicious pivots |
| 616 |
ncol, |
| 617 |
one, // alpha, |
| 618 |
a_diag, |
| 619 |
nrow_block_diag, // 3 Dec.2015 |
| 620 |
a_lower, |
| 621 |
nrow_block_lower); // 3 Dec.2015 |
| 622 |
} |
| 623 |
for (int i = 0; i < nrow; i++) { |
| 624 |
const T aa = D.diag(i1 + i); |
| 625 |
for (int j = 0; j < ncol; j++) { |
| 626 |
a_upper[i + j * nrow] *= aa; |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
template |
| 632 |
void C_dinvDL_timesU<double>(void *arg_); |
| 633 |
|
| 634 |
template |
| 635 |
void C_dinvDL_timesU<quadruple>(void *arg_); |
| 636 |
|
| 637 |
template |
| 638 |
void C_dinvDL_timesU<complex<double> >(void *arg_); |
| 639 |
|
| 640 |
template |
| 641 |
void C_dinvDL_timesU<complex<quadruple> >(void *arg_); |
| 642 |
|
| 643 |
template |
| 644 |
void C_dinvDL_timesU<float>(void *arg_); |
| 645 |
|
| 646 |
template |
| 647 |
void C_dinvDL_timesU<complex<float> >(void *arg_); |
| 648 |
|
| 649 |
// |
| 650 |
|
| 651 |
template<typename T, typename U> |
| 652 |
void C_gauss_whole_pivot(void *arg_) |
| 653 |
{ |
| 654 |
const T zero(0.0); |
| 655 |
const T one(1.0); |
| 656 |
C_dfull_gauss_arg<T, U> *arg = (C_dfull_gauss_arg<T, U> *)arg_; |
| 657 |
|
| 658 |
const int task_position = arg->task_position; |
| 659 |
const int n = arg->n; |
| 660 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 661 |
ColumnMatrix<T> &a = *(arg->a); |
| 662 |
const double eps_piv = *(arg->eps_piv); |
| 663 |
|
| 664 |
double pivot = *(arg->pivot); |
| 665 |
|
| 666 |
// double *a_sym = D.addrCoefs(); |
| 667 |
vector<int>& permute = D.getPermute(); |
| 668 |
vector<int>& singIdx0 = D.getSingIdx0(); |
| 669 |
const int aug_dim = *(arg->aug_dim); |
| 670 |
const U eps_machine = *(arg->eps_machine); |
| 671 |
const bool verbose = arg->verbose; |
| 672 |
FILE *fp = *(arg->fp); |
| 673 |
int n1, dim_kern; |
| 674 |
|
| 675 |
|
| 676 |
if (task_position % 2 == 1) { |
| 677 |
ColumnMatrix<T> aa(n, n); |
| 678 |
aa.copy(a); // blas_copy<T>((n * n), a.addrCoefs(), 1, aa, 1); |
| 679 |
if (arg->isSym) { |
| 680 |
// copy upper to lower for rank-1 update procedure by dsyr('L', ) |
| 681 |
for (int j = 0; j < n; j++) { |
| 682 |
for (int i = 0; i < j; i++) { |
| 683 |
// aa[j + i * n] = aa[i + j * n]; |
| 684 |
aa(j, i) = aa(i, j); |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
const double pivot0 = *(arg->pivot0); |
| 689 |
const double pivot1 = *(arg->pivot1); |
| 690 |
pivot = (pivot0 < pivot1 ? pivot0 : pivot1); |
| 691 |
int n0 = 0; |
| 692 |
diss_printf(verbose, fp, |
| 693 |
"%s %d : C_gauss_whole_pivot : serial factroization : n = %d\n", |
| 694 |
__FILE__, __LINE__, n); |
| 695 |
int count_repeat = 0; |
| 696 |
bool flag_repeat_piv = true; |
| 697 |
double eps_piv1 = eps_piv; |
| 698 |
while (flag_repeat_piv) { |
| 699 |
if (count_repeat > 0) { |
| 700 |
aa.copy(a); |
| 701 |
// blas_copy<T>((n * n), a.addrCoefs(), 1, aa.addrCoefs(), 1); |
| 702 |
if (arg->isSym) { |
| 703 |
// copy upper to lower for rank-1 update procedure by dsyr('L', ) |
| 704 |
for (int j = 0; j < n; j++) { |
| 705 |
for (int i = 0; i < j; i++) { |
| 706 |
aa[j + i * n] = aa[i + j * n]; |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
} // if (count_repeat > 0) |
| 711 |
n0 = 0; |
| 712 |
full_gauss3<T, U>(&n0, aa.addrCoefs(), n, &pivot, &permute[0], |
| 713 |
arg->isSym, |
| 714 |
eps_piv1, |
| 715 |
verbose, |
| 716 |
fp); |
| 717 |
diss_printf(verbose, fp, |
| 718 |
"%s %d : C_gauss_whole_pivot : pivot = %g n0 = %d\n", |
| 719 |
__FILE__, __LINE__, pivot, n0); |
| 720 |
if (((n - n0) >= aug_dim) || (eps_piv1 < TOL_PIVOT)) { |
| 721 |
flag_repeat_piv = false; |
| 722 |
} |
| 723 |
else { |
| 724 |
eps_piv1 /= 10.0; |
| 725 |
count_repeat++; |
| 726 |
} |
| 727 |
} // while (flag_repeat_piv) |
| 728 |
diss_printf(verbose, fp, |
| 729 |
"%s %d : eps_pvi = %g pivot = %g n0 = %d count_repeat = %d\n", |
| 730 |
__FILE__, __LINE__, eps_piv1, pivot, n0, count_repeat); |
| 731 |
// D.unsetBlocked(); // |
| 732 |
D.copyFromArray(aa.addrCoefs(), n); |
| 733 |
// modification of lower blocks by removing D^-1 in the same mannar of |
| 734 |
// factorization by blocks |
| 735 |
if (!arg->isSym) { |
| 736 |
const int num_block = D.num_blocks(); |
| 737 |
for (int k = 0; k < num_block; k++) { |
| 738 |
for (int m = (k + 1); m < num_block; m++) { |
| 739 |
T *lower = D.addrCoefBlock(m, k); |
| 740 |
const int nrow = D.nrowBlock(m, k); |
| 741 |
const int ncol = D.ncolBlock(m, k); |
| 742 |
for (int i = 0; i < nrow; i++) { |
| 743 |
const T aa = one / D.addrCoefBlock(k, k)[i * (nrow + 1)]; |
| 744 |
for (int j = 0; j < ncol; j++) { |
| 745 |
lower[i + j * nrow] *= aa; |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
// |
| 752 |
dim_kern = 0; |
| 753 |
if (n0 > 0) { |
| 754 |
singIdx0.resize(n0); |
| 755 |
// singular nodes are continuously located from the last |
| 756 |
for (int i = 0; i < n0; i++) { |
| 757 |
singIdx0[i] = n - n0 + i; |
| 758 |
} |
| 759 |
dim_kern = dimKernDense<T, U>(singIdx0, n, |
| 760 |
aug_dim, |
| 761 |
eps_machine, |
| 762 |
eps_piv, |
| 763 |
D, |
| 764 |
a.addrCoefs(), |
| 765 |
false, // refactorize |
| 766 |
true, // isFullPermute |
| 767 |
arg->isSym, |
| 768 |
verbose, |
| 769 |
fp); |
| 770 |
dim_kern = n0; |
| 771 |
diss_printf(verbose, fp, |
| 772 |
"%s %d : C_gauss_whole_pivot : dim_kern = %d\n", |
| 773 |
__FILE__, __LINE__, dim_kern); |
| 774 |
if (dim_kern == (-1)) { |
| 775 |
diss_printf(verbose, fp, |
| 776 |
"%s %d : strict diagonal pivot is not enough!\n", |
| 777 |
__FILE__, __LINE__); |
| 778 |
dim_kern = n0; |
| 779 |
D.set_KernelDetected(false); |
| 780 |
} |
| 781 |
else { |
| 782 |
n0 = dim_kern; |
| 783 |
D.set_KernelDetected(true); |
| 784 |
} |
| 785 |
} // if (n0 > 0) |
| 786 |
aa.free(); // need to move after copy to SquareBlockMatrix D(,) |
| 787 |
D.unsetBlocked(); // |
| 788 |
if (n0 > 0) { |
| 789 |
n1 = n - n0; // n - dim_kern; ==> regular part needs to be restored |
| 790 |
// nullify lower part corresponding to the suspicious pivots |
| 791 |
for (int j = 0; j < n; j++) { |
| 792 |
for (int i = n1; i < n; i++){ |
| 793 |
a[i + j * n] = zero; |
| 794 |
} |
| 795 |
} |
| 796 |
if (!arg->isSym) { |
| 797 |
for (int j = 0; j < n; j++) { |
| 798 |
for (int i = n1; i < n; i++){ |
| 799 |
a[j + i * n] = zero; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
D.getSingIdx().resize(n0); |
| 804 |
for (int i = 0; i < n0; i++) { |
| 805 |
D.getSingIdx()[i] = permute[singIdx0[i]]; |
| 806 |
} |
| 807 |
// |
| 808 |
D.set_lastPivot(pivot); |
| 809 |
D.set_dim_kern(dim_kern); |
| 810 |
D.set_rank(n - dim_kern); |
| 811 |
} // if (n0 > 0) |
| 812 |
else { |
| 813 |
singIdx0.resize(0); |
| 814 |
D.getSingIdx().resize(0); |
| 815 |
D.set_KernelDetected(true); |
| 816 |
D.set_lastPivot(pivot); |
| 817 |
D.set_dim_kern(0); |
| 818 |
D.set_rank(n); |
| 819 |
} // if (n0 > 0) |
| 820 |
{ |
| 821 |
int dim_kern1 = dim_kern; |
| 822 |
for (int k = (D.num_blocks() - 1); k >= 0 ; k--) { |
| 823 |
int nrow_local = D.nrowBlock(k); |
| 824 |
int dim_kern2 = nrow_local < dim_kern1 ? nrow_local : dim_kern1; |
| 825 |
D.set_dim_kern_block(k, dim_kern2); |
| 826 |
if (dim_kern1 > nrow_local) { |
| 827 |
dim_kern1 -= nrow_local; |
| 828 |
} |
| 829 |
else { |
| 830 |
dim_kern1 = 0; |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
#ifdef DEBUG_MATRIX_DFULLLDLT |
| 835 |
cout << arg->task_name << "n= " << n << endl; |
| 836 |
for (int i = 0; i < n; i++) { |
| 837 |
cout << " " << permute[i]; |
| 838 |
} |
| 839 |
cout << endl; |
| 840 |
for (int i = 0; i < ((n * (n + 1)) / 2); i++) { |
| 841 |
cout << " " << a_sym[i]; |
| 842 |
} |
| 843 |
cout << endl; |
| 844 |
#endif |
| 845 |
} // if (task_position % 2 == 1) |
| 846 |
*(arg->quit) = true; |
| 847 |
arg->a->free(); |
| 848 |
#ifdef DEBUG_MEMORY_ALLOC |
| 849 |
cerr << "C_gauss_whole_pivot : memory deallocate." << endl; |
| 850 |
// |
| 851 |
cout << "pivot " << pivot << endl; |
| 852 |
#endif |
| 853 |
*(arg->pivot) = pivot; // for passing information to other task |
| 854 |
|
| 855 |
} |
| 856 |
|
| 857 |
template |
| 858 |
void C_gauss_whole_pivot<double, double>(void *arg_); |
| 859 |
|
| 860 |
template |
| 861 |
void C_gauss_whole_pivot<complex<double>, double>(void *arg_); |
| 862 |
|
| 863 |
template |
| 864 |
void C_gauss_whole_pivot<quadruple, quadruple>(void *arg_); |
| 865 |
|
| 866 |
template |
| 867 |
void C_gauss_whole_pivot<complex<quadruple>, quadruple>(void *arg_); |
| 868 |
|
| 869 |
template |
| 870 |
void C_gauss_whole_pivot<float, float>(void *arg_); |
| 871 |
|
| 872 |
template |
| 873 |
void C_gauss_whole_pivot<complex<float>, float>(void *arg_); |
| 874 |
|
| 875 |
// |
| 876 |
|
| 877 |
template<typename T> |
| 878 |
void C_dupdateb_Schur_offdiag_t(void *arg_) |
| 879 |
{ |
| 880 |
C_dupdateb_Schur_arg<T> *arg = (C_dupdateb_Schur_arg<T> *)arg_; |
| 881 |
|
| 882 |
const int nrow = arg->nrow; |
| 883 |
const int ncol = arg->ncol; |
| 884 |
const int b_size = arg->b_size; |
| 885 |
SquareBlockMatrix<T> &D = *(arg->D); |
| 886 |
|
| 887 |
const int i1 = D.IndexBlock(arg->i1_block); // arg->i1_block * SIZE_B1; |
| 888 |
const int ii = D.IndexBlock(arg->ii_block); // arg->ii_block * SIZE_B1; |
| 889 |
const int jj = D.IndexBlock(arg->jj_block); // arg->jj_block * SIZE_B1; |
| 890 |
FILE *fp = *(arg->fp); |
| 891 |
const bool verbose = arg->verbose; |
| 892 |
diss_printf(verbose, fp, |
| 893 |
"C_dupdate_Schur_offdiag_t : i1=%d ii=%d jj=%d nrow=%d ncol=%d b_size=%d\n", |
| 894 |
i1, ii, jj, nrow, ncol, b_size); |
| 895 |
diss_printf(verbose, fp, "no computation\n"); |
| 896 |
} |
| 897 |
|
| 898 |
template |
| 899 |
void C_dupdateb_Schur_offdiag_t<double>(void *arg_); |
| 900 |
|
| 901 |
template |
| 902 |
void C_dupdateb_Schur_offdiag_t<quadruple>(void *arg_); |
| 903 |
|
| 904 |
template |
| 905 |
void C_dupdateb_Schur_offdiag_t<complex<double> >(void *arg_); |
| 906 |
|
| 907 |
template |
| 908 |
void C_dupdateb_Schur_offdiag_t<complex<quadruple> >(void *arg_); |
| 909 |
|
| 910 |
template |
| 911 |
void C_dupdateb_Schur_offdiag_t<float>(void *arg_); |
| 912 |
|
| 913 |
template |
| 914 |
void C_dupdateb_Schur_offdiag_t<complex<float> >(void *arg_); |
| 915 |
// |