| 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 shylu_iqr_driver.cpp |
| 11 |
|
| 12 |
\brief Factors and solves a sparse matrix using ShyLU and IQR, if enabled. |
| 13 |
|
| 14 |
\author Radu Popescu <[email protected]> |
| 15 |
|
| 16 |
* The example needs the following Ifpack variables enabled when compiling Trilinos: |
| 17 |
-DIfpack_ENABLE_DYNAMIC_FACTORY:BOOL=ON (allows the use of the new factory which can register |
| 18 |
new preconditioners within the client application) |
| 19 |
-DIfpack_ENABLE_PARALLEL_SUBDOMAIN_SOLVERS:BOOL=ON (allows using MPI parallel subdomain |
| 20 |
solvers for Ifpack AAS) |
| 21 |
* The example doesn't fail if these variables are disabled, but it doesn't do anything |
| 22 |
* interesting, it just solves the problem with Ifpack AAS with serial Amesos subdomain solvers. |
| 23 |
|
| 24 |
* Highlights: |
| 25 |
* There is a builder function for Ifpack_ShyLU declared at the beginning of the file. |
| 26 |
* Ifpack_ShyLU is registered with the Ifpack_DynamicFactory class at the beginning of the |
| 27 |
main() function, using the builder function that was declared earlier. |
| 28 |
* An alternate XML file is provided in case the CMake variables mentioned earlier are not |
| 29 |
enabled, to prevent the test from failing. |
| 30 |
* A global Teuchos parameter list is read from an XML file, with sublists for the matrix |
| 31 |
partitioner, the preconditioner and the linear solver |
| 32 |
* The ML list contains a sublist for Ifpack (as usual), which in turn contains a sublist for |
| 33 |
all ShyLU parameters (this behaviour is enabled with the Ifpack_DynamicFactory switch, to |
| 34 |
avoid parameter list polution) |
| 35 |
* Through Ifpack_DynamicFactory, ML is be able to build the Ifpack_AdditiveSchwarz<Ifpack_ShyLU> |
| 36 |
preconditioner. |
| 37 |
|
| 38 |
* Key parameters: |
| 39 |
* Isorropia parameters: |
| 40 |
* we set the partitioning method to HIER_GRAPH, to ensure that the parts which make up each |
| 41 |
AAS subdomain are connected |
| 42 |
* we set TOPOLOGY to the number of processor per AAS subdomain |
| 43 |
* ML parameters: |
| 44 |
* smoother: ifpack type - string - should be set to ShyLU |
| 45 |
* smoother: ifpack overlap - int - 0 - using multiple processors per AAS subdomain forces |
| 46 |
this. Overlap is not supported. |
| 47 |
* Ifpack parameters: |
| 48 |
* subdomain: number-of-processors - int - number of processors per AAS subdomain (must be a |
| 49 |
divisor of the total number of MPI processes used). |
| 50 |
* ShyLU parameters: |
| 51 |
* Schur Approximation Method - usually A22AndBlockDiagonals; can be set to IQR or G. IQR |
| 52 |
means that we use IQR to solve the Schur complement system inexactly (Krylov subspace |
| 53 |
reuse method), G means that we just approximate the inverse of the Schur complement with |
| 54 |
an AAS of the G subblock of the subdomain matrix (which is faster, but leads to a looser |
| 55 |
coupling at the subdomain level, for some problems it leads to fewer outer GMRES iterations |
| 56 |
than IQR). |
| 57 |
* IQR Initial Prec Type - string - default: Amesos (this is the actual string given to an |
| 58 |
Ifpack factory; it means AAS with serial Amesos on subdomains) - the preconditioner used |
| 59 |
for the GMRES solver within IQR. This is also used for the approximation of the Schur |
| 60 |
complement inverse when Schur Approximation Method is set to G. |
| 61 |
* IQR Initial Prec Amesos Type - string - default: Amesos_Klu - which Amesos solver to use |
| 62 |
for the IQR preconditioner |
| 63 |
* Amesos_Klu is given as a default in multiple places of the XML file. Should be substituted |
| 64 |
with faster alternatives: Amesos_Pardiso, Amesos_Umfpack etc. |
| 65 |
|
| 66 |
\remark Usage: |
| 67 |
\code mpirun -n 2np ShyLU_iqr_driver.exe |
| 68 |
|
| 69 |
*/ |
| 70 |
|
| 71 |
#include <string> |
| 72 |
|
| 73 |
#include <Epetra_ConfigDefs.h> |
| 74 |
#ifdef HAVE_MPI |
| 75 |
#include <Epetra_MpiComm.h> |
| 76 |
#else |
| 77 |
#include <Epetra_SerialComm.h> |
| 78 |
#endif |
| 79 |
#include <Epetra_CrsMatrix.h> |
| 80 |
|
| 81 |
#include <Epetra_Vector.h> |
| 82 |
#include <EpetraExt_CrsMatrixIn.h> |
| 83 |
#include "EpetraExt_MultiVectorIn.h" |
| 84 |
#include "EpetraExt_BlockMapIn.h" |
| 85 |
|
| 86 |
#include <BelosConfigDefs.hpp> |
| 87 |
#include <BelosLinearProblem.hpp> |
| 88 |
#include <BelosEpetraAdapter.hpp> |
| 89 |
#include <BelosBlockGmresSolMgr.hpp> |
| 90 |
|
| 91 |
#include <Ifpack_ConfigDefs.h> |
| 92 |
#if defined(HAVE_IFPACK_DYNAMIC_FACTORY) && defined(HAVE_IFPACK_PARALLEL_SUBDOMAIN_SOLVERS) |
| 93 |
#include <Ifpack_DynamicFactory.h> |
| 94 |
#include <Ifpack_AdditiveSchwarz.h> |
| 95 |
#include <Ifpack_ShyLU.h> |
| 96 |
#else |
| 97 |
#include <Ifpack.h> |
| 98 |
#endif |
| 99 |
|
| 100 |
#include <Isorropia_EpetraPartitioner.hpp> |
| 101 |
#include <Isorropia_EpetraRedistributor.hpp> |
| 102 |
|
| 103 |
#include <ml_MultiLevelPreconditioner.h> |
| 104 |
|
| 105 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 106 |
#include <Teuchos_ParameterList.hpp> |
| 107 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 108 |
#include <Teuchos_Time.hpp> |
| 109 |
|
| 110 |
namespace |
| 111 |
{ |
| 112 |
typedef Epetra_MultiVector MV; |
| 113 |
typedef Epetra_Operator OP; |
| 114 |
|
| 115 |
#if defined(HAVE_IFPACK_DYNAMIC_FACTORY) && defined(HAVE_IFPACK_PARALLEL_SUBDOMAIN_SOLVERS) |
| 116 |
// Declare Ifpack_ShyLU builder function for later registration |
| 117 |
Ifpack_Preconditioner* buildShyLU(Epetra_RowMatrix* Matrix, |
| 118 |
int Overlap, |
| 119 |
bool /*serial*/, |
| 120 |
bool /*overrideSerialDefault*/) |
| 121 |
{ |
| 122 |
return new Ifpack_AdditiveSchwarz<Ifpack_ShyLU>(Matrix, Overlap); |
| 123 |
} |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
using Teuchos::RCP; |
| 128 |
using Teuchos::rcp; |
| 129 |
using Teuchos::ParameterList; |
| 130 |
|
| 131 |
int InitMatValues( const Epetra_CrsMatrix& newA, Epetra_CrsMatrix* A ); |
| 132 |
int InitMVValues( const Epetra_MultiVector& newb, Epetra_MultiVector* b ); |
| 133 |
|
| 134 |
int main(int argc, char** argv) |
| 135 |
{ |
| 136 |
// Initialize MPI environment |
| 137 |
#ifdef HAVE_MPI |
| 138 |
MPI_Init(&argc, &argv); |
| 139 |
Epetra_MpiComm Comm(MPI_COMM_WORLD); |
| 140 |
#else |
| 141 |
Epetra_SerialComm Comm; |
| 142 |
#endif |
| 143 |
|
| 144 |
bool success = true; |
| 145 |
std::string pass = "End Result: TEST PASSED"; |
| 146 |
std::string fail = "End Result: TEST FAILED"; |
| 147 |
|
| 148 |
int nProcs = Comm.NumProc(); |
| 149 |
int myPID = Comm.MyPID(); |
| 150 |
bool verbose = (myPID == 0); |
| 151 |
|
| 152 |
if (verbose) { |
| 153 |
std::cout << "--- Running ShyLU-IQR test with " << nProcs << |
| 154 |
" processes." << std::endl; |
| 155 |
} |
| 156 |
|
| 157 |
// Register Ifpack_ShyLU with the Ifpack factory. |
| 158 |
#if defined(HAVE_IFPACK_DYNAMIC_FACTORY) && defined(HAVE_IFPACK_PARALLEL_SUBDOMAIN_SOLVERS) |
| 159 |
{ |
| 160 |
int err = Ifpack_DynamicFactory::RegisterPreconditioner("ShyLU", |
| 161 |
buildShyLU); |
| 162 |
if (! err) { |
| 163 |
if (verbose) { |
| 164 |
std::cout << "--- Ifpack_ShyLU was registered with" |
| 165 |
<<" Ifpack_DynamicFactory" << std::endl; |
| 166 |
} |
| 167 |
} else { |
| 168 |
if (verbose) { |
| 169 |
std::cout << "!!! Error registering preconditioner ShyLU with" |
| 170 |
<< " Ifpack_DynamicFactory. Exiting." << std::endl; |
| 171 |
} |
| 172 |
std::cout << fail << std::endl; |
| 173 |
#ifdef HAVE_MPI |
| 174 |
MPI_Finalize(); |
| 175 |
#endif |
| 176 |
return -1; |
| 177 |
} |
| 178 |
} |
| 179 |
#endif |
| 180 |
|
| 181 |
// Read the file containing the test parameters |
| 182 |
#if defined(HAVE_IFPACK_DYNAMIC_FACTORY) && defined(HAVE_IFPACK_PARALLEL_SUBDOMAIN_SOLVERS) |
| 183 |
std::string parameterFileName = "shylu_iqr_parameters.xml"; |
| 184 |
#else |
| 185 |
std::string parameterFileName = "shylu_no_iqr_parameters.xml"; |
| 186 |
#endif |
| 187 |
|
| 188 |
// Accept an alternate XML parameter file name, read from the command line |
| 189 |
{ |
| 190 |
Teuchos::CommandLineProcessor cl(false, false); |
| 191 |
cl.setDocString (""); |
| 192 |
cl.setOption ("parameter-file", |
| 193 |
¶meterFileName, |
| 194 |
"The name of the XML test parameter file"); |
| 195 |
Teuchos::CommandLineProcessor::EParseCommandLineReturn parseReturn = |
| 196 |
cl.parse (argc, argv); |
| 197 |
if ( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED) { |
| 198 |
std::cout << fail << std::endl; |
| 199 |
#ifdef HAVE_MPI |
| 200 |
MPI_Finalize(); |
| 201 |
#endif |
| 202 |
return 0; |
| 203 |
} |
| 204 |
if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) { |
| 205 |
std::cout << fail << std::endl; |
| 206 |
#ifdef HAVE_MPI |
| 207 |
MPI_Finalize(); |
| 208 |
#endif |
| 209 |
return -2; |
| 210 |
} |
| 211 |
} |
| 212 |
if (verbose) { |
| 213 |
std::cout << "--- Using parameter file: " << parameterFileName << |
| 214 |
std::endl; |
| 215 |
} |
| 216 |
|
| 217 |
// Read the XML parameter file. The resulting Teuchos parameter list |
| 218 |
// contains sublists for |
| 219 |
// the partitioner, the preconditioner, linear solver etc. |
| 220 |
RCP<ParameterList> globalParams = rcp(new ParameterList()); |
| 221 |
globalParams = Teuchos::getParametersFromXmlFile(parameterFileName); |
| 222 |
|
| 223 |
// Read the matrix |
| 224 |
std::string matrixFileName |
| 225 |
= globalParams->get<std::string>("matrix file name", "wathenSmall.mtx"); |
| 226 |
std::string rhsFileName = globalParams->get<std::string>("rhs_file", ""); |
| 227 |
std::string mapFileName = globalParams->get<std::string>("map_file", ""); |
| 228 |
|
| 229 |
int maxFiles = globalParams->get<int>("Maximum number of files to read in", |
| 230 |
1); |
| 231 |
int startFile = globalParams->get<int>("Number of initial file", 1); |
| 232 |
int file_number = startFile; |
| 233 |
|
| 234 |
char file_name[200]; |
| 235 |
if (mapFileName != "" && maxFiles > 1) { |
| 236 |
mapFileName += "%d.mm"; |
| 237 |
sprintf( file_name, mapFileName.c_str(), file_number ); |
| 238 |
} |
| 239 |
else { |
| 240 |
strcpy( file_name, mapFileName.c_str()); |
| 241 |
} |
| 242 |
if (verbose) { |
| 243 |
std::cout << "--- Using map file: " << file_name << std::endl; |
| 244 |
} |
| 245 |
|
| 246 |
bool mapAvail = false; |
| 247 |
Epetra_BlockMap *vecMap2 = NULL; |
| 248 |
if (mapFileName != "") |
| 249 |
{ |
| 250 |
mapAvail = true; |
| 251 |
int err = EpetraExt::MatrixMarketFileToBlockMap(file_name, Comm, vecMap2); |
| 252 |
if (err) { |
| 253 |
if (verbose) { |
| 254 |
std::cout << "!!! Matrix file could not be read in, info = "<< |
| 255 |
err << std::endl; |
| 256 |
} |
| 257 |
std::cout << fail << std::endl; |
| 258 |
#ifdef HAVE_MPI |
| 259 |
MPI_Finalize(); |
| 260 |
#endif |
| 261 |
return -3; |
| 262 |
} |
| 263 |
} |
| 264 |
Epetra_Map* vecMap = static_cast<Epetra_Map *>( vecMap2 ); |
| 265 |
|
| 266 |
|
| 267 |
if (maxFiles > 1) { |
| 268 |
matrixFileName += "%d.mm"; |
| 269 |
sprintf( file_name, matrixFileName.c_str(), file_number ); |
| 270 |
} |
| 271 |
else { |
| 272 |
strcpy( file_name, matrixFileName.c_str()); |
| 273 |
} |
| 274 |
|
| 275 |
if (verbose) { |
| 276 |
std::cout << "--- Using matrix file: " << file_name << std::endl; |
| 277 |
} |
| 278 |
|
| 279 |
Epetra_CrsMatrix *A; |
| 280 |
int err; |
| 281 |
if (mapAvail) { |
| 282 |
err = EpetraExt::MatrixMarketFileToCrsMatrix(file_name, *vecMap, A); |
| 283 |
} |
| 284 |
else { |
| 285 |
err = EpetraExt::MatrixMarketFileToCrsMatrix(file_name, Comm, A); |
| 286 |
} |
| 287 |
if (err) { |
| 288 |
if (verbose) { |
| 289 |
std::cout << "!!! Matrix file could not be read in, info = "<< |
| 290 |
err << std::endl; |
| 291 |
} |
| 292 |
std::cout << fail << std::endl; |
| 293 |
#ifdef HAVE_MPI |
| 294 |
MPI_Finalize(); |
| 295 |
#endif |
| 296 |
return -3; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
if (rhsFileName != "" && maxFiles > 1) |
| 301 |
{ |
| 302 |
rhsFileName += "%d.mm"; |
| 303 |
sprintf( file_name, rhsFileName.c_str(), file_number ); |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
strcpy( file_name, rhsFileName.c_str()); |
| 308 |
} |
| 309 |
if (verbose) { |
| 310 |
std::cout << "--- Using rhs file: " << file_name << std::endl; |
| 311 |
} |
| 312 |
|
| 313 |
// Partition the matrix |
| 314 |
ParameterList isorropiaParams = globalParams->sublist |
| 315 |
("Isorropia parameters"); |
| 316 |
RCP<Isorropia::Epetra::Partitioner> partitioner |
| 317 |
= rcp(new Isorropia::Epetra::Partitioner(A, isorropiaParams, false), |
| 318 |
true); |
| 319 |
partitioner->partition(); |
| 320 |
RCP<Isorropia::Epetra::Redistributor> rd |
| 321 |
= rcp(new Isorropia::Epetra::Redistributor(partitioner)); |
| 322 |
|
| 323 |
int numRows = A->NumGlobalRows(); |
| 324 |
RCP<Epetra_Map> vectorMap = rcp(new Epetra_Map(numRows, 0, Comm)); |
| 325 |
Epetra_MultiVector *RHS; |
| 326 |
bool allOneRHS = false; |
| 327 |
|
| 328 |
if (rhsFileName != "") |
| 329 |
{ |
| 330 |
if (mapAvail) { |
| 331 |
err = EpetraExt::MatrixMarketFileToMultiVector(file_name, *vecMap, RHS); |
| 332 |
} |
| 333 |
else { |
| 334 |
err = EpetraExt::MatrixMarketFileToMultiVector(file_name, *vectorMap, RHS); |
| 335 |
} |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
// Generate a RHS and LHS |
| 340 |
allOneRHS = true; |
| 341 |
if (mapAvail) { |
| 342 |
RHS = new Epetra_MultiVector(*vecMap, 1, false); |
| 343 |
} |
| 344 |
else { |
| 345 |
RHS = new Epetra_MultiVector(*vectorMap, 1, false); |
| 346 |
} |
| 347 |
RHS->Random(); |
| 348 |
} |
| 349 |
|
| 350 |
Epetra_MultiVector *LHS; |
| 351 |
if (mapAvail) { |
| 352 |
LHS = new Epetra_MultiVector(*vecMap, 1, true); |
| 353 |
} |
| 354 |
else { |
| 355 |
LHS = new Epetra_MultiVector(*vectorMap, 1, true); |
| 356 |
} |
| 357 |
LHS->PutScalar(0.0); |
| 358 |
|
| 359 |
// Redistribute matrix and vectors |
| 360 |
RCP<Epetra_CrsMatrix> rcpA; |
| 361 |
RCP<Epetra_MultiVector> rcpRHS; |
| 362 |
RCP<Epetra_MultiVector> rcpLHS; |
| 363 |
Epetra_CrsMatrix *newA; |
| 364 |
Epetra_MultiVector *newLHS, *newRHS; |
| 365 |
|
| 366 |
if (mapAvail) { |
| 367 |
rcpA = rcp(A, true); |
| 368 |
rcpRHS = rcp(RHS, true); |
| 369 |
rcpLHS = rcp(LHS, true); |
| 370 |
} |
| 371 |
else { |
| 372 |
rd->redistribute(*A, newA); |
| 373 |
delete A; |
| 374 |
rcpA = rcp(newA, true); |
| 375 |
|
| 376 |
rd->redistribute(*RHS, newRHS); |
| 377 |
delete RHS; |
| 378 |
rcpRHS = rcp(newRHS, true); |
| 379 |
|
| 380 |
rd->redistribute(*LHS, newLHS); |
| 381 |
delete LHS; |
| 382 |
rcpLHS = rcp(newLHS, true); |
| 383 |
} |
| 384 |
|
| 385 |
if (! globalParams->isSublist("ML parameters")) { |
| 386 |
if (verbose) { |
| 387 |
std::cout << "!!! ML parameter list not found. Exiting." |
| 388 |
<< std::endl; |
| 389 |
} |
| 390 |
std::cout << fail << std::endl; |
| 391 |
#ifdef HAVE_MPI |
| 392 |
MPI_Finalize(); |
| 393 |
#endif |
| 394 |
return -4; |
| 395 |
} |
| 396 |
ParameterList mlParameters = globalParams->sublist("ML parameters"); |
| 397 |
|
| 398 |
if (! globalParams->isSublist("Belos parameters")) { |
| 399 |
if (verbose) { |
| 400 |
std::cout << "!!! Belos parameter list not found. Exiting." << |
| 401 |
std::endl; |
| 402 |
} |
| 403 |
std::cout << fail << std::endl; |
| 404 |
#ifdef HAVE_MPI |
| 405 |
MPI_Finalize(); |
| 406 |
#endif |
| 407 |
return -5; |
| 408 |
} |
| 409 |
ParameterList belosParams = globalParams->sublist("Belos parameters"); |
| 410 |
int belosBlockSize = belosParams.get<int>("Block Size", 1); |
| 411 |
int belosMaxRestarts = belosParams.get<int>("Maximum Restarts", 0); |
| 412 |
int belosMaxIterations = belosParams.get<int>("Maximum Iterations", |
| 413 |
numRows); |
| 414 |
double belosTolerance = belosParams.get<double>("Convergence Tolerance", |
| 415 |
1e-10); |
| 416 |
if (verbose) { |
| 417 |
std::cout << std::endl; |
| 418 |
std::cout << "--- Dimension of matrix: " << numRows << std::endl; |
| 419 |
std::cout << "--- Block size used by solver: " << belosBlockSize << |
| 420 |
std::endl; |
| 421 |
std::cout << "--- Number of restarts allowed: " << belosMaxRestarts |
| 422 |
<< std::endl; |
| 423 |
std::cout << "--- Max number of Gmres iterations per restart cycle: " |
| 424 |
<< belosMaxIterations << std::endl; |
| 425 |
std::cout << "--- Relative residual tolerance: " << belosTolerance |
| 426 |
<< std::endl; |
| 427 |
} |
| 428 |
|
| 429 |
Epetra_CrsMatrix *iterA = 0; |
| 430 |
Epetra_CrsMatrix *redistA = 0; |
| 431 |
Epetra_MultiVector *iterb1 = 0; |
| 432 |
RCP<ML_Epetra::MultiLevelPreconditioner> MLprec; |
| 433 |
Teuchos::Time setupPrecTimer("preconditioner setup timer", false); |
| 434 |
Teuchos::Time linearSolverTimer("linear solver timer"); |
| 435 |
RCP< Belos::SolverManager<double, MV, OP> > solver; |
| 436 |
while(file_number < maxFiles+startFile) |
| 437 |
{ |
| 438 |
/*if (file_number == startFile) |
| 439 |
{*/ |
| 440 |
// Build ML preconditioner |
| 441 |
if (mapAvail) |
| 442 |
{ |
| 443 |
setupPrecTimer.start(); |
| 444 |
MLprec = rcp(new ML_Epetra::MultiLevelPreconditioner(*A, |
| 445 |
mlParameters, false), true); |
| 446 |
MLprec->ComputePreconditioner(); |
| 447 |
setupPrecTimer.stop(); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
setupPrecTimer.start(); |
| 452 |
MLprec = rcp(new ML_Epetra::MultiLevelPreconditioner(*newA, |
| 453 |
mlParameters, false), true); |
| 454 |
MLprec->ComputePreconditioner(); |
| 455 |
setupPrecTimer.stop(); |
| 456 |
} |
| 457 |
/*} |
| 458 |
else |
| 459 |
{ |
| 460 |
setupPrecTimer.start(); |
| 461 |
MLprec->ReComputePreconditioner(); |
| 462 |
setupPrecTimer.stop(); |
| 463 |
}*/ |
| 464 |
|
| 465 |
// Build linear solver |
| 466 |
|
| 467 |
RCP<Belos::EpetraPrecOp> belosPrec = rcp(new Belos::EpetraPrecOp(MLprec), |
| 468 |
false); |
| 469 |
|
| 470 |
// Construct a preconditioned linear problem |
| 471 |
RCP<Belos::LinearProblem<double, MV, OP> > problem |
| 472 |
= rcp( new Belos::LinearProblem<double, MV, OP>( rcpA, rcpLHS, rcpRHS ) ); |
| 473 |
problem->setRightPrec( belosPrec ); |
| 474 |
|
| 475 |
if (! problem->setProblem()) { |
| 476 |
if (verbose) { |
| 477 |
std::cout << "!!! Belos::LinearProblem failed to set up correctly." |
| 478 |
<< " Exiting." << std::endl; |
| 479 |
} |
| 480 |
std::cout << fail << std::endl; |
| 481 |
#ifdef HAVE_MPI |
| 482 |
MPI_Finalize(); |
| 483 |
#endif |
| 484 |
return -6; |
| 485 |
} |
| 486 |
|
| 487 |
// Create an iterative solver manager |
| 488 |
solver = rcp( new Belos::BlockGmresSolMgr<double, MV, OP>(problem, |
| 489 |
rcp(&belosParams,false))); |
| 490 |
// Solve linear system |
| 491 |
linearSolverTimer.start(); |
| 492 |
Belos::ReturnType ret = solver->solve(); |
| 493 |
linearSolverTimer.stop(); |
| 494 |
if (ret == Belos::Unconverged) { |
| 495 |
if (verbose) { |
| 496 |
std::cout << "!!! Linear solver did not converge to prescribed" |
| 497 |
<<" precision. Test failed." << std::endl; |
| 498 |
} |
| 499 |
std::cout << fail << std::endl; |
| 500 |
#ifdef HAVE_MPI |
| 501 |
MPI_Finalize(); |
| 502 |
#endif |
| 503 |
return -7; |
| 504 |
} |
| 505 |
// Print time measurements |
| 506 |
int numIters = solver->getNumIters(); |
| 507 |
double timeSetupPrec = setupPrecTimer.totalElapsedTime(); |
| 508 |
double timeLinearSolver = linearSolverTimer.totalElapsedTime(); |
| 509 |
if (verbose) { |
| 510 |
std::cout << "--- Preconditioner setup time: " << timeSetupPrec << |
| 511 |
std::endl; |
| 512 |
std::cout << "--- Number of iterations performed: " << numIters << |
| 513 |
std::endl; |
| 514 |
std::cout << "--- Time to GMRES convergence: " << timeLinearSolver << |
| 515 |
std::endl; |
| 516 |
std::cout << "--- Average time per GMRES iteration: " |
| 517 |
<< timeLinearSolver / numIters << std::endl; |
| 518 |
std::cout << "--- Total time to solution (prec + GMRES): " |
| 519 |
<< timeSetupPrec + timeLinearSolver << std::endl; |
| 520 |
} |
| 521 |
|
| 522 |
file_number++; |
| 523 |
if (file_number >= maxFiles+startFile) |
| 524 |
{ |
| 525 |
if (redistA != NULL) { delete redistA; redistA = NULL; } |
| 526 |
if (iterb1 != NULL){delete iterb1; iterb1 = NULL;} |
| 527 |
break; |
| 528 |
} |
| 529 |
else |
| 530 |
{ |
| 531 |
sprintf(file_name, matrixFileName.c_str(), file_number); |
| 532 |
|
| 533 |
if (redistA != NULL) { delete redistA; redistA = NULL; } |
| 534 |
// Load the new matrix |
| 535 |
if (mapAvail) { |
| 536 |
err = EpetraExt::MatrixMarketFileToCrsMatrix |
| 537 |
(file_name, *vecMap, redistA); |
| 538 |
} |
| 539 |
else { |
| 540 |
err = EpetraExt::MatrixMarketFileToCrsMatrix(file_name, Comm, |
| 541 |
iterA); |
| 542 |
} |
| 543 |
if (err != 0) { |
| 544 |
if (myPID == 0) std::cout << "Could not open file: "<< file_name << std::endl; |
| 545 |
success = false; |
| 546 |
} |
| 547 |
else |
| 548 |
{ |
| 549 |
if (mapAvail) |
| 550 |
{ |
| 551 |
InitMatValues(*redistA, A); |
| 552 |
if (redistA != NULL) { delete redistA; redistA = NULL; } |
| 553 |
} |
| 554 |
else { |
| 555 |
rd->redistribute(*iterA, redistA); |
| 556 |
if (iterA != NULL){delete iterA; iterA = NULL;} |
| 557 |
InitMatValues(*redistA, newA); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
// Load the new rhs |
| 562 |
if (!allOneRHS) |
| 563 |
{ |
| 564 |
sprintf(file_name, rhsFileName.c_str(), file_number); |
| 565 |
|
| 566 |
if (iterb1 != NULL){delete iterb1; iterb1 = NULL;} |
| 567 |
if (mapAvail) { |
| 568 |
err = EpetraExt::MatrixMarketFileToMultiVector(file_name, |
| 569 |
*vecMap, iterb1); |
| 570 |
} |
| 571 |
else { |
| 572 |
err = EpetraExt::MatrixMarketFileToMultiVector(file_name, |
| 573 |
*vectorMap, RHS); |
| 574 |
} |
| 575 |
if (err != 0) { |
| 576 |
if (myPID==0) |
| 577 |
std::cout << "Could not open file: "<< file_name << std::endl; |
| 578 |
success = false; |
| 579 |
} |
| 580 |
else { |
| 581 |
if (mapAvail) { |
| 582 |
InitMVValues( *iterb1, RHS ); |
| 583 |
if (iterb1 != NULL){ delete iterb1; iterb1 = NULL; } |
| 584 |
} |
| 585 |
else { |
| 586 |
rd->redistribute(*RHS, iterb1); |
| 587 |
if (RHS != NULL){ delete RHS; RHS = NULL; } |
| 588 |
InitMVValues( *iterb1, newRHS ); |
| 589 |
// Should we delete iterb1 |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
// Release the ML preconditioner, destroying MPI subcommunicators before the call to |
| 597 |
// MPI_Finalize() in order to avoid MPI errors (this is related to ParMETIS, I think). |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
if(success) |
| 602 |
std::cout << pass << std::endl; |
| 603 |
else |
| 604 |
std::cout << fail << std::endl; |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
MLprec.reset(); |
| 609 |
#ifdef HAVE_MPI |
| 610 |
MPI_Finalize(); |
| 611 |
#endif |
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
int InitMatValues( const Epetra_CrsMatrix& newA, Epetra_CrsMatrix* A ) |
| 617 |
{ |
| 618 |
int numMyRows = newA.NumMyRows(); |
| 619 |
int maxNum = newA.MaxNumEntries(); |
| 620 |
int numIn; |
| 621 |
int *idx = 0; |
| 622 |
double *vals = 0; |
| 623 |
|
| 624 |
idx = new int[maxNum]; |
| 625 |
vals = new double[maxNum]; |
| 626 |
|
| 627 |
// For each row get the values and indices, and replace the values in A. |
| 628 |
for (int i=0; i<numMyRows; ++i) { |
| 629 |
|
| 630 |
// Get the values and indices from newA. |
| 631 |
EPETRA_CHK_ERR( newA.ExtractMyRowCopy(i, maxNum, numIn, vals, idx) ); |
| 632 |
|
| 633 |
// Replace the values in A |
| 634 |
EPETRA_CHK_ERR( A->ReplaceMyValues(i, numIn, vals, idx) ); |
| 635 |
|
| 636 |
} |
| 637 |
|
| 638 |
// Clean up. |
| 639 |
delete [] idx; |
| 640 |
delete [] vals; |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
int InitMVValues( const Epetra_MultiVector& newb, Epetra_MultiVector* b ) |
| 646 |
{ |
| 647 |
int length = newb.MyLength(); |
| 648 |
int numVecs = newb.NumVectors(); |
| 649 |
const Epetra_Vector *tempnewvec; |
| 650 |
Epetra_Vector *tempvec = 0; |
| 651 |
|
| 652 |
for (int i=0; i<numVecs; ++i) { |
| 653 |
tempnewvec = newb(i); |
| 654 |
tempvec = (*b)(i); |
| 655 |
for (int j=0; j<length; ++j) |
| 656 |
(*tempvec)[j] = (*tempnewvec)[j]; |
| 657 |
} |
| 658 |
|
| 659 |
return 0; |
| 660 |
} |