Debian Codegraph @

packages/shylu/shylu_dd/core/test/shylu_driver.cpp

raw
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_driver.cpp
11
12 \brief Factors and solves a sparse matrix using LU factorization.
13
14 \author Siva Rajamanickam
15
16 \remark Usage:
17 \code mpirun -n np shylu_driver.exe
18
19 */
20
21 #include <assert.h>
22 #include <iostream>
23 #include <sstream>
24
25 #include "Isorropia_config.h" // Just for HAVE_MPI
26
27 // Epetra includes
28 #ifdef HAVE_MPI
29 # include "Epetra_MpiComm.h"
30 #endif // HAVE_MPI
31 #include "Epetra_SerialComm.h"
32 #include "Epetra_CrsMatrix.h"
33 #include "Epetra_Map.h"
34 #include "Epetra_MultiVector.h"
35 #include "Epetra_LinearProblem.h"
36
37 // Teuchos includes
38 #include "Teuchos_GlobalMPISession.hpp"
39 #include "Teuchos_XMLParameterListHelpers.hpp"
40
41 // EpetraExt includes
42 #include "EpetraExt_RowMatrixOut.h"
43 #include "EpetraExt_MultiVectorOut.h"
44 #include "EpetraExt_CrsMatrixIn.h"
45 #include "EpetraExt_MultiVectorIn.h"
46
47 // Amesos includes
48 #include "Amesos.h"
49 #include "Amesos_BaseSolver.h"
50
51 // AztecOO includes
52 #include "AztecOO.h"
53
54 // ML includes
55 #include "ml_MultiLevelPreconditioner.h"
56 #include "ml_epetra_utils.h"
57
58 #include "shylu.h"
59 #include "shylu_util.h"
60 #include "Ifpack_ShyLU.h"
61 #include "Ifpack_ILUT.h"
62 #include "Ifpack_ILU.h"
63
64 using namespace std;
65
66 int main(int argc, char *argv[])
67 {
68 #ifdef HAVE_MPI
69 Teuchos::GlobalMPISession mpiSession(&argc, &argv, 0);
70 Epetra_MpiComm Comm(MPI_COMM_WORLD);
71 #else
72 Epetra_SerialComm Comm;
73 #endif
74 int nProcs, myPID ;
75 Teuchos::ParameterList pLUList ; // ParaLU parameters
76 Teuchos::ParameterList isoList ; // Isorropia parameters
77 Teuchos::ParameterList shyLUList ; // shyLU parameters
78 Teuchos::ParameterList ifpackList ; // shyLU parameters
79 string ipFileName = "ShyLU.xml"; // TODO : Accept as i/p
80
81 nProcs = mpiSession.getNProc();
82 myPID = Comm.MyPID();
83
84 if (myPID == 0)
85 {
86 cout <<"Parallel execution: nProcs="<< nProcs << endl;
87 }
88
89 // =================== Read input xml file =============================
90 Teuchos::updateParametersFromXmlFile(ipFileName, &pLUList);
91 isoList = pLUList.sublist("Isorropia Input");
92 shyLUList = pLUList.sublist("ShyLU Input");
93 shyLUList.set("Outer Solver Library", "AztecOO");
94 // Get matrix market file name
95 string MMFileName = Teuchos::getParameter<string>(pLUList, "mm_file");
96 string prec_type = Teuchos::getParameter<string>(pLUList, "preconditioner");
97 int maxiters = Teuchos::getParameter<int>(pLUList, "Outer Solver MaxIters");
98 double tol = Teuchos::getParameter<double>(pLUList, "Outer Solver Tolerance");
99 string rhsFileName = pLUList.get<string>("rhs_file", "");
100
101 if (myPID == 0)
102 {
103 cout << "Input :" << endl;
104 cout << "ParaLU params " << endl;
105 pLUList.print(std::cout, 2, true, true);
106 cout << "Matrix market file name: " << MMFileName << endl;
107 }
108
109 // ==================== Read input Matrix ==============================
110 Epetra_CrsMatrix *A;
111 Epetra_MultiVector *b1;
112
113 int err = EpetraExt::MatrixMarketFileToCrsMatrix(MMFileName.c_str(), Comm,
114 A);
115 //EpetraExt::MatlabFileToCrsMatrix(MMFileName.c_str(), Comm, A);
116 //assert(err != 0);
117 //cout <<"Done reading the matrix"<< endl;
118 int n = A->NumGlobalRows();
119 //cout <<"n="<< n << endl;
120
121 // Create input vectors
122 Epetra_Map vecMap(n, 0, Comm);
123 if (rhsFileName != "")
124 {
125 err = EpetraExt::MatrixMarketFileToMultiVector(rhsFileName.c_str(),
126 vecMap, b1);
127 }
128 else
129 {
130 b1 = new Epetra_MultiVector(vecMap, 1, false);
131 b1->PutScalar(1.0);
132 }
133
134 Epetra_MultiVector x(vecMap, 1);
135 //cout << "Created the vectors" << endl;
136
137 // Partition the matrix with hypergraph partitioning and redisstribute
138 Isorropia::Epetra::Partitioner *partitioner = new
139 Isorropia::Epetra::Partitioner(A, isoList, false);
140 partitioner->partition();
141 Isorropia::Epetra::Redistributor rd(partitioner);
142
143 Epetra_CrsMatrix *newA;
144 Epetra_MultiVector *newX, *newB;
145 rd.redistribute(*A, newA);
146 delete A;
147 A = newA;
148
149 rd.redistribute(x, newX);
150 rd.redistribute(*b1, newB);
151
152 Epetra_LinearProblem problem(A, newX, newB);
153
154 AztecOO solver(problem);
155
156 ifpackList ;
157 Ifpack_Preconditioner *prec;
158 ML_Epetra::MultiLevelPreconditioner *MLprec;
159 if (prec_type.compare("ShyLU") == 0)
160 {
161 prec = new Ifpack_ShyLU(A);
162 prec->SetParameters(shyLUList);
163 prec->Initialize();
164 prec->Compute();
165 //(dynamic_cast<Ifpack_ShyLU *>(prec))->JustTryIt();
166 //cout << " Going to set it in solver" << endl ;
167 solver.SetPrecOperator(prec);
168 //cout << " Done setting the solver" << endl ;
169 }
170 else if (prec_type.compare("ILU") == 0)
171 {
172 ifpackList.set( "fact: level-of-fill", 1 );
173 prec = new Ifpack_ILU(A);
174 prec->SetParameters(ifpackList);
175 prec->Initialize();
176 prec->Compute();
177 solver.SetPrecOperator(prec);
178 }
179 else if (prec_type.compare("ILUT") == 0)
180 {
181 ifpackList.set( "fact: ilut level-of-fill", 2 );
182 ifpackList.set( "fact: drop tolerance", 1e-8);
183 prec = new Ifpack_ILUT(A);
184 prec->SetParameters(ifpackList);
185 prec->Initialize();
186 prec->Compute();
187 solver.SetPrecOperator(prec);
188 }
189 else if (prec_type.compare("ML") == 0)
190 {
191 Teuchos::ParameterList mlList; // TODO : Take it from i/p
192 MLprec = new ML_Epetra::MultiLevelPreconditioner(*A, mlList, true);
193 solver.SetPrecOperator(MLprec);
194 }
195
196 solver.SetAztecOption(AZ_solver, AZ_gmres);
197 solver.SetMatrixName(333);
198 //solver.SetAztecOption(AZ_output, 1);
199 //solver.SetAztecOption(AZ_conv, AZ_Anorm);
200 //cout << "Going to iterate for the global problem" << endl;
201
202 solver.Iterate(maxiters, tol);
203
204 // compute ||Ax - b||
205 double Norm;
206 Epetra_MultiVector Ax(vecMap, 1);
207
208 Epetra_MultiVector *newAx;
209 rd.redistribute(Ax, newAx);
210 A->Multiply(false, *newX, *newAx);
211 newAx->Update(1.0, *newB, -1.0);
212 newAx->Norm2(&Norm);
213 double ANorm = A->NormOne();
214
215 cout << "|Ax-b |/|A| = " << Norm/ANorm << endl;
216
217 delete newAx;
218 if (prec_type.compare("ML") == 0)
219 {
220 delete MLprec;
221 }
222 else
223 {
224 delete prec;
225 }
226
227 delete b1;
228 delete newX;
229 delete newB;
230 delete A;
231 delete partitioner;
232 }