Debian Codegraph @

dune/istl/paamg/test/parallelamgtest.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifdef TEST_AGGLO
6 #define UNKNOWNS 10
7 #endif
8 #include "anisotropic.hh"
9 #include <dune/common/timer.hh>
10 #include <dune/common/parallel/indexset.hh>
11 #include <dune/common/parallel/mpicommunication.hh>
12 #include <dune/istl/paamg/amg.hh>
13 #include <dune/istl/paamg/pinfo.hh>
14 #include <dune/istl/schwarz.hh>
15 #include <dune/istl/owneroverlapcopy.hh>
16 #include <string>
17
18 template<class T, class C>
19 class DoubleStepPreconditioner
20 : public Dune::Preconditioner<typename T::domain_type, typename T::range_type>
21 {
22 public:
23 typedef typename T::domain_type X;
24 typedef typename T::range_type Y;
25
26 enum {category = T::category};
27
28 DoubleStepPreconditioner(T& preconditioner_, C& comm)
29 : preconditioner(&preconditioner_), comm_(comm)
30 {}
31
32 virtual void pre (X& x, Y& b)
33 {
34 preconditioner->pre(x,b);
35 }
36
37 virtual void apply(X& v, const Y& d)
38 {
39 preconditioner->apply(v,d);
40 comm_.copyOwnerToAll(v,v);
41 }
42
43 virtual void post (X& x)
44 {
45 preconditioner->post(x);
46 }
47 private:
48 T* preconditioner;
49 C& comm_;
50 };
51
52
53 class MPIError {
54 public:
55 /** @brief Constructor. */
56 MPIError(std::string s, int e) : errorstring(s), errorcode(e){}
57 /** @brief The error string. */
58 std::string errorstring;
59 /** @brief The mpi error code. */
60 int errorcode;
61 };
62
63 void MPI_err_handler([[maybe_unused]] MPI_Comm *comm, int *err_code, ...)
64 {
65 char *err_string=new char[MPI_MAX_ERROR_STRING];
66 int err_length;
67 MPI_Error_string(*err_code, err_string, &err_length);
68 std::string s(err_string, err_length);
69 std::cerr << "An MPI Error occurred:"<<std::endl<<s<<std::endl;
70 delete[] err_string;
71 throw MPIError(s, *err_code);
72 }
73
74 template<int BS>
75 void testAmg(int N, int coarsenTarget)
76 {
77 std::cout<<"==================================================="<<std::endl;
78 std::cout<<"BS="<<BS<<" N="<<N<<" coarsenTarget="<<coarsenTarget<<std::endl;
79
80 int procs, rank;
81 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
82 MPI_Comm_size(MPI_COMM_WORLD, &procs);
83
84 typedef Dune::FieldMatrix<double,BS,BS> MatrixBlock;
85 typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat;
86 typedef Dune::FieldVector<double,BS> VectorBlock;
87 typedef Dune::BlockVector<VectorBlock> Vector;
88 typedef int GlobalId;
89 typedef Dune::OwnerOverlapCopyCommunication<GlobalId> Communication;
90 typedef Dune::OverlappingSchwarzOperator<BCRSMat,Vector,Vector,Communication> Operator;
91 int n;
92
93 N/=BS;
94
95 Communication comm(MPI_COMM_WORLD);
96
97 BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, comm.indexSet(), comm.communicator(), &n, 1);
98
99 const BCRSMat& cmat = mat;
100
101 comm.remoteIndices().template rebuild<false>();
102
103 Vector b(cmat.N()), x(cmat.M());
104
105 b=0;
106 x=100;
107
108 setBoundary(x, b, N, comm.indexSet());
109
110 if(N<=6) {
111 std::ostringstream name;
112 name<<rank<<": row";
113
114 Dune::printmatrix(std::cout, cmat, "A", name.str().c_str());
115 Dune::printvector(std::cout, x, "x", name.str().c_str());
116 //Dune::printvector(std::cout, b, "b", name.str().c_str());
117 }
118
119 Dune::Timer watch;
120
121 watch.reset();
122 Operator fop(cmat, comm);
123
124 typedef Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> >
125 Criterion;
126 typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> Smoother;
127 //typedef Dune::SeqJac<BCRSMat,Vector,Vector> Smoother;
128 //typedef Dune::SeqILU0<BCRSMat,Vector,Vector> Smoother;
129 //typedef Dune::SeqILUn<BCRSMat,Vector,Vector> Smoother;
130 typedef Dune::BlockPreconditioner<Vector,Vector,Communication,Smoother> ParSmoother;
131 typedef typename Dune::Amg::SmootherTraits<ParSmoother>::Arguments SmootherArgs;
132
133 Dune::OverlappingSchwarzScalarProduct<Vector,Communication> sp(comm);
134
135 Dune::InverseOperatorResult r, r1;
136
137 double buildtime;
138
139 SmootherArgs smootherArgs;
140
141 smootherArgs.iterations = 1;
142
143
144 Criterion criterion(15,coarsenTarget);
145 criterion.setDefaultValuesIsotropic(2);
146
147
148 typedef Dune::Amg::AMG<Operator,Vector,ParSmoother,Communication> AMG;
149
150 AMG amg(fop, criterion, smootherArgs, comm);
151
152 buildtime = watch.elapsed();
153
154 if(rank==0)
155 std::cout<<"Building hierarchy took "<<buildtime<<" seconds"<<std::endl;
156
157 Dune::CGSolver<Vector> amgCG(fop, sp, amg, 10e-8, 300, (rank==0) ? 2 : 0);
158 watch.reset();
159
160 amgCG.apply(x,b,r);
161 amg.recalculateHierarchy();
162
163
164 MPI_Barrier(MPI_COMM_WORLD);
165 double solvetime = watch.elapsed();
166
167 b=0;
168 x=100;
169
170 setBoundary(x, b, N, comm.indexSet());
171
172 Dune::CGSolver<Vector> amgCG1(fop, sp, amg, 10e-8, 300, (rank==0) ? 2 : 0);
173 amgCG1.apply(x,b,r);
174
175 if(!r.converged && rank==0)
176 std::cerr<<" AMG Cg solver did not converge!"<<std::endl;
177
178 if(rank==0) {
179 std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl;
180
181 std::cout<<"AMG building took "<<(buildtime/r.elapsed*r.iterations)<<" iterations"<<std::endl;
182 std::cout<<"AMG building together with slving took "<<buildtime+solvetime<<std::endl;
183 }
184
185 }
186
187 template<int BSStart, int BSEnd, int BSStep=1>
188 struct AMGTester
189 {
190 static void test(int N, int coarsenTarget)
191 {
192 testAmg<BSStart>(N, coarsenTarget);
193 const int next = (BSStart+BSStep>BSEnd) ? BSEnd : BSStart+BSStep;
194 AMGTester<next,BSEnd,BSStep>::test(N, coarsenTarget);
195 }
196 }
197 ;
198
199 template<int BSStart,int BSStep>
200 struct AMGTester<BSStart,BSStart,BSStep>
201 {
202 static void test(int N, int coarsenTarget)
203 {
204 testAmg<BSStart>(N, coarsenTarget);
205 }
206 };
207
208
209 int main(int argc, char** argv)
210 {
211 MPI_Init(&argc, &argv);
212 MPI_Errhandler handler;
213 MPI_Comm_create_errhandler(MPI_err_handler, &handler);
214 MPI_Comm_set_errhandler(MPI_COMM_WORLD, handler);
215
216 int N=100;
217
218 int coarsenTarget=200;
219
220 if(argc>1)
221 N = atoi(argv[1]);
222
223 if(argc>2)
224 coarsenTarget = atoi(argv[2]);
225
226 #ifdef TEST_AGGLO
227 N=UNKNOWNS;
228 #endif
229 AMGTester<1,1>::test(N, coarsenTarget);
230 //AMGTester<1,5>::test(N, coarsenTarget);
231 // AMGTester<10,10>::test(N, coarsenTarget);
232
233 MPI_Finalize();
234 }