Debian Codegraph @

packages/shylu/shylu_dd/core/test/amesos_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 amesos_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 amesos_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 #include "Teuchos_Time.hpp"
41
42 // EpetraExt includes
43 #include "EpetraExt_RowMatrixOut.h"
44 #include "EpetraExt_MultiVectorOut.h"
45 #include "EpetraExt_CrsMatrixIn.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 using namespace std;
59
60 int main(int argc, char *argv[])
61 {
62 #ifdef HAVE_MPI
63 Teuchos::GlobalMPISession mpiSession(&argc, &argv, 0);
64 Epetra_MpiComm Comm(MPI_COMM_WORLD);
65 #else
66 Epetra_SerialComm Comm;
67 #endif
68 int nProcs, myPID ;
69 Teuchos::ParameterList pLUList ; // ParaLU parameters
70 Teuchos::ParameterList isoList ; // Isorropia parameters
71 string ipFileName = "ShyLU.xml"; // TODO : Accept as i/p
72
73 nProcs = mpiSession.getNProc();
74 myPID = Comm.MyPID();
75
76 if (myPID == 0)
77 {
78 cout <<"Parallel execution: nProcs="<< nProcs << endl;
79 }
80
81 // =================== Read input xml file =============================
82 Teuchos::updateParametersFromXmlFile(ipFileName, &pLUList);
83 isoList = pLUList.sublist("Isorropia Input");
84 // Get matrix market file name
85 string MMFileName = Teuchos::getParameter<string>(pLUList, "mm_file");
86 string prec_type = Teuchos::getParameter<string>(pLUList, "preconditioner");
87
88 if (myPID == 0)
89 {
90 cout << "Input :" << endl;
91 cout << "ParaLU params " << endl;
92 pLUList.print(std::cout, 2, true, true);
93 cout << "Matrix market file name: " << MMFileName << endl;
94 }
95
96 // ==================== Read input Matrix ==============================
97 Epetra_CrsMatrix *A;
98
99 int err = EpetraExt::MatrixMarketFileToCrsMatrix(MMFileName.c_str(), Comm, A);
100 //EpetraExt::MatlabFileToCrsMatrix(MMFileName.c_str(), Comm, A);
101 //assert(err != 0);
102 cout <<"Done reading the matrix"<< endl;
103 int n = A->NumGlobalRows();
104 cout <<"n="<< n << endl;
105
106 // Create input vectors
107 Epetra_Map vecMap(n, 0, Comm);
108 Epetra_MultiVector x(vecMap, 1);
109 Epetra_MultiVector b(vecMap, 1, false);
110 b.PutScalar(1.0); // TODO : Accept it as input
111
112 // Partition the matrix with hypergraph partitioning and redisstribute
113 Isorropia::Epetra::Partitioner *partitioner = new
114 Isorropia::Epetra::Partitioner(A, isoList, false);
115 partitioner->partition();
116 Isorropia::Epetra::Redistributor rd(partitioner);
117
118 Epetra_CrsMatrix *newA;
119 Epetra_MultiVector *newX, *newB;
120 rd.redistribute(*A, newA);
121 delete A;
122 A = newA;
123
124 rd.redistribute(x, newX);
125 rd.redistribute(b, newB);
126
127 Epetra_LinearProblem problem(A, newX, newB);
128
129 Amesos Factory;
130 char* SolverType = "Amesos_Klu";
131 bool IsAvailable = Factory.Query(SolverType);
132
133 Epetra_LinearProblem *LP = new Epetra_LinearProblem();
134 LP->SetOperator(A);
135 LP->SetLHS(newX);
136 LP->SetRHS(newB);
137 Amesos_BaseSolver *Solver = Factory.Create(SolverType, *LP);
138
139
140 Solver->SymbolicFactorization();
141 Teuchos::Time ftime("setup time");
142 ftime.start();
143 Solver->NumericFactorization();
144 cout << "Numeric Factorization" << endl;
145 Solver->Solve();
146 cout << "Solve done" << endl;
147
148 ftime.stop();
149 cout << "Time to setup" << ftime.totalElapsedTime() << endl;
150
151 // compute ||Ax - b||
152 double Norm;
153 Epetra_MultiVector Ax(vecMap, 1);
154
155 Epetra_MultiVector *newAx;
156 rd.redistribute(Ax, newAx);
157 A->Multiply(false, *newX, *newAx);
158 newAx->Update(1.0, *newB, -1.0);
159 newAx->Norm2(&Norm);
160 double ANorm = A->NormOne();
161
162 cout << "|Ax-b |/|A| = " << Norm/ANorm << endl;
163
164 delete newAx;
165 delete newX;
166 delete newB;
167 delete A;
168 delete partitioner;
169 }