| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// ShyLU: Scalable Hybrid LU Preconditioner and Solver |
| 4 |
// |
| 5 |
// Copyright 2011 NTESS and the ShyLU contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/** \file Ifpack_ShyLU.cpp |
| 11 |
|
| 12 |
\brief Use ShyLU as a preconditioner within IFPACK. |
| 13 |
|
| 14 |
\author Siva Rajamanickam |
| 15 |
|
| 16 |
*/ |
| 17 |
|
| 18 |
#include "Ifpack_ShyLU.h" |
| 19 |
#include "Teuchos_Time.hpp" |
| 20 |
#include "Ifpack_config.h" |
| 21 |
|
| 22 |
#include <IQRSolver.h> |
| 23 |
|
| 24 |
//#define DUMP_MATRICES |
| 25 |
|
| 26 |
Ifpack_ShyLU::Ifpack_ShyLU(Epetra_CrsMatrix* A): |
| 27 |
A_(A), |
| 28 |
IsParallel_(true), |
| 29 |
IsInitialized_(false), |
| 30 |
IsComputed_(false), |
| 31 |
Label_("Ifpack_ShyLU"), |
| 32 |
NumCompute_(0), |
| 33 |
NumApplyInverse_(0), |
| 34 |
Time_(A_->Comm()) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
void Ifpack_ShyLU::Destroy() |
| 39 |
{ |
| 40 |
if (IsInitialized_) |
| 41 |
{ |
| 42 |
//delete A_; |
| 43 |
//delete partitioner_; |
| 44 |
//delete rd_; |
| 45 |
|
| 46 |
// I would rather explicitly delete |
| 47 |
/*delete slu_sym_.LP; |
| 48 |
delete slu_sym_.Solver; |
| 49 |
delete slu_sym_.C; |
| 50 |
delete slu_sym_.R; |
| 51 |
delete slu_sym_.D; |
| 52 |
delete slu_sym_.G; |
| 53 |
delete slu_sym_.Sg; |
| 54 |
delete slu_sym_.prober;*/ |
| 55 |
|
| 56 |
if (slu_config_.schurSolver == "Amesos") |
| 57 |
{ |
| 58 |
delete slu_data_.dsolver; |
| 59 |
} |
| 60 |
else if (slu_config_.libName == "Belos") |
| 61 |
delete slu_data_.innersolver; |
| 62 |
|
| 63 |
delete[] slu_data_.DRowElems; |
| 64 |
delete[] slu_data_.SRowElems; |
| 65 |
delete[] slu_data_.DColElems; |
| 66 |
delete[] slu_data_.gvals; |
| 67 |
} |
| 68 |
if (IsComputed_) |
| 69 |
{ |
| 70 |
// I would rather explicitly delete |
| 71 |
// delete slu_data_.Sbar; |
| 72 |
slu_data_.localSbargraph = Teuchos::null; |
| 73 |
slu_data_.guided_prober = Teuchos::null; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
int Ifpack_ShyLU::Initialize() |
| 78 |
{ |
| 79 |
if(Comm().NumProc() != 1) |
| 80 |
IsParallel_ = true; |
| 81 |
else |
| 82 |
IsParallel_ = false; |
| 83 |
|
| 84 |
// TODO: |
| 85 |
// Need to enable partitioning here in Initialize once moving to Belos |
| 86 |
|
| 87 |
// Cannot call this method , need the partitioner around TODO : Can we |
| 88 |
// avoid this |
| 89 |
//A_ = balanceAndRedistribute(A_, List_); |
| 90 |
|
| 91 |
// ==================== Symbolic factorization ========================= |
| 92 |
// 1. Partition and redistribute [ |
| 93 |
//partitioner_ = new Isorropia::Epetra::Partitioner(A_, List_, false); |
| 94 |
//partitioner_->partition(); |
| 95 |
|
| 96 |
//rd_ = new Isorropia::Epetra::Redistributor(partitioner_); |
| 97 |
//Epetra_CrsMatrix *newA; |
| 98 |
//rd_->redistribute(*A_, newA); |
| 99 |
//A_ = newA; |
| 100 |
// ] |
| 101 |
|
| 102 |
/*double Sdiagfactor = 0.05; // hard code the diagonals |
| 103 |
shylu_factor(A_, 1, LP_, Solver_, C_, Dnr_, DRowElems_, Snr_, SRowElems_, |
| 104 |
Sbar_, Sdiagfactor);*/ |
| 105 |
slu_config_.sym = Teuchos::getParameter<int>(List_, |
| 106 |
"Symmetry"); |
| 107 |
slu_config_.libName = Teuchos::getParameter<std::string>(List_, |
| 108 |
"Outer Solver Library"); |
| 109 |
std::string schurApproxMethod = Teuchos::getParameter<std::string>(List_, |
| 110 |
"Schur Approximation Method"); |
| 111 |
slu_config_.schurSolver = Teuchos::getParameter<std::string>(List_, |
| 112 |
"Schur Complement Solver"); |
| 113 |
slu_config_.schurAmesosSolver = List_.get<std::string>("Schur Amesos Solver", |
| 114 |
"Amesos_Klu"); |
| 115 |
//slu_config_.diagonalBlockSolver = |
| 116 |
//Teuchos::getParameter<std::string>(List_, "Diagonal Block Solver"); |
| 117 |
slu_config_.diagonalBlockSolver = List_.get<std::string>("Diagonal Block Solver", |
| 118 |
"Amesos_Klu"); |
| 119 |
|
| 120 |
|
| 121 |
slu_config_.relative_threshold = 0.0; |
| 122 |
slu_config_.Sdiagfactor = 0.05; |
| 123 |
if (schurApproxMethod == "A22AndBlockDiagonals") |
| 124 |
{ |
| 125 |
slu_config_.schurApproxMethod = 1; |
| 126 |
slu_config_.Sdiagfactor = Teuchos::getParameter<double>(List_, |
| 127 |
"Diagonal Factor"); |
| 128 |
} |
| 129 |
else if (schurApproxMethod == "Threshold") |
| 130 |
{ |
| 131 |
slu_config_.schurApproxMethod = 2; |
| 132 |
slu_config_.relative_threshold = Teuchos::getParameter<double>(List_, |
| 133 |
"Relative Threshold"); |
| 134 |
} |
| 135 |
else if (schurApproxMethod == "Guided Probing") |
| 136 |
{ |
| 137 |
slu_config_.schurApproxMethod = 3; |
| 138 |
slu_config_.reset_iter = List_.get<int>("Schur Recompute Iteration", 10); |
| 139 |
slu_config_.relative_threshold = Teuchos::getParameter<double>(List_, |
| 140 |
"Relative Threshold"); |
| 141 |
} |
| 142 |
if (schurApproxMethod == "IQR") |
| 143 |
{ |
| 144 |
slu_config_.schurSolver = "IQR"; |
| 145 |
slu_config_.schurApproxMethod = 4; |
| 146 |
|
| 147 |
List_.set<bool>("Use full IQR", true, ""); |
| 148 |
slu_data_.iqrSolver.reset(new IQR::IQRSolver(List_)); |
| 149 |
} |
| 150 |
if (schurApproxMethod == "G") |
| 151 |
{ |
| 152 |
slu_config_.schurSolver = "G"; |
| 153 |
slu_config_.schurApproxMethod = 5; |
| 154 |
|
| 155 |
List_.set<bool>("Use full IQR", false, ""); |
| 156 |
slu_data_.iqrSolver.reset(new IQR::IQRSolver(List_)); |
| 157 |
} |
| 158 |
|
| 159 |
slu_config_.schurPreconditioner = List_.get<std::string>("Schur Preconditioner", |
| 160 |
"ILU stand-alone"); |
| 161 |
slu_config_.silent_subiter = List_.get<bool>("Silent subiterations", |
| 162 |
true); |
| 163 |
|
| 164 |
slu_config_.inner_tolerance = Teuchos::getParameter<double>(List_, |
| 165 |
"Inner Solver Tolerance"); |
| 166 |
slu_config_.inner_maxiters = Teuchos::getParameter<int>(List_, |
| 167 |
"Inner Solver MaxIters"); |
| 168 |
slu_config_.overlap = List_.get<int>("Schur Preconditioner Overlap", 0); |
| 169 |
std::string sep_type = Teuchos::getParameter<std::string>(List_, |
| 170 |
"Separator Type"); |
| 171 |
// mfh 25 May 2015: dl is unused (don't declare it, or you'll get |
| 172 |
// build warnings). However, two-argument get() has side effects, |
| 173 |
// so I don't want to comment it out. |
| 174 |
// |
| 175 |
//int dl = List_.get<int>("Debug Level", 0); |
| 176 |
(void) List_.get<int>("Debug Level", 0); |
| 177 |
//slu_config_.dm.setDebugLevel(dl); |
| 178 |
|
| 179 |
if (sep_type == "Wide") |
| 180 |
slu_config_.sep_type = 1; |
| 181 |
else |
| 182 |
slu_config_.sep_type = 2; |
| 183 |
|
| 184 |
shylu_symbolic_factor(A_, &slu_sym_, &slu_data_, &slu_config_); |
| 185 |
|
| 186 |
if (slu_config_.schurSolver == "Amesos") |
| 187 |
{ |
| 188 |
slu_data_.LP2 = Teuchos::rcp(new Epetra_LinearProblem()); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
if (slu_config_.libName == "Belos") |
| 193 |
slu_data_.innersolver = new AztecOO() ; |
| 194 |
else |
| 195 |
slu_data_.innersolver = NULL; |
| 196 |
} |
| 197 |
|
| 198 |
IsInitialized_ = true; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
int Ifpack_ShyLU::SetParameters(Teuchos::ParameterList& parameterlist) |
| 203 |
{ |
| 204 |
#ifdef HAVE_IFPACK_DYNAMIC_FACTORY |
| 205 |
List_ = parameterlist.sublist("ShyLU list", true); |
| 206 |
#else |
| 207 |
List_ = parameterlist; |
| 208 |
#endif |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
int Ifpack_ShyLU::Compute() |
| 213 |
{ |
| 214 |
Teuchos::Time ftime("setup time"); |
| 215 |
ftime.start(); |
| 216 |
|
| 217 |
slu_data_.num_compute = NumCompute_; |
| 218 |
|
| 219 |
shylu_factor(A_, &slu_sym_, &slu_data_, &slu_config_); |
| 220 |
|
| 221 |
ftime.stop(); |
| 222 |
IsComputed_ = true; |
| 223 |
NumCompute_++; |
| 224 |
//cout << " Done with the compute" << endl ; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
int Ifpack_ShyLU::JustTryIt() |
| 229 |
{ |
| 230 |
// Dummy function, To show the error in AztecOO, This works |
| 231 |
//cout << "Entering JustTryIt" << endl; |
| 232 |
AztecOO *solver; |
| 233 |
solver = slu_data_.innersolver; |
| 234 |
//cout << solver_ << endl; |
| 235 |
Epetra_Map BsMap(-1, slu_data_.Snr, slu_data_.SRowElems, 0, A_->Comm()); |
| 236 |
Epetra_MultiVector Xs(BsMap, 1); |
| 237 |
Epetra_MultiVector Bs(BsMap, 1); |
| 238 |
Xs.PutScalar(0.0); |
| 239 |
solver->SetLHS(&Xs); |
| 240 |
solver->SetRHS(&Bs); |
| 241 |
solver->Iterate(30, 1e-10); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
int Ifpack_ShyLU::ApplyInverse(const Epetra_MultiVector& X, |
| 246 |
Epetra_MultiVector& Y) const |
| 247 |
{ |
| 248 |
#ifdef DUMP_MATRICES |
| 249 |
if (NumApplyInverse_ == 0) |
| 250 |
{ |
| 251 |
EpetraExt::MultiVectorToMatlabFile("X.mat", X); |
| 252 |
} |
| 253 |
#endif |
| 254 |
//cout << "Entering ApplyInvers" << endl; |
| 255 |
|
| 256 |
shylu_solve(&slu_sym_, &slu_data_, &slu_config_, X, Y); |
| 257 |
#ifdef DUMP_MATRICES |
| 258 |
if (NumApplyInverse_ == 0) |
| 259 |
{ |
| 260 |
EpetraExt::MultiVectorToMatlabFile("Y.mat", Y); |
| 261 |
} |
| 262 |
#endif |
| 263 |
NumApplyInverse_++; |
| 264 |
//cout << "Leaving ApplyInvers" << endl; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
//! Computes the estimated condition number and returns the value. |
| 269 |
double Ifpack_ShyLU::Condest(const Ifpack_CondestType CT, |
| 270 |
const int MaxIters, const double Tol, Epetra_RowMatrix* Matrix_in) |
| 271 |
{ |
| 272 |
return -1.0; |
| 273 |
} |
| 274 |
|
| 275 |
//! Prints on stream basic information about \c this object. |
| 276 |
std::ostream& Ifpack_ShyLU::Print(std::ostream& os) const |
| 277 |
{ |
| 278 |
os << " !!!!!!!!! " << std::endl; |
| 279 |
return os; |
| 280 |
} |