Debian Codegraph @

dune/istl/paamg/test/pthreadamgtest.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
6 /*#include"xreal.h"
7
8 namespace std
9 {
10
11 HPA::xreal abs(const HPA::xreal& t)
12 {
13 return t>=0?t:-t;
14 }
15
16 };
17 */
18
19 #include "anisotropic.hh"
20 #include <dune/common/timer.hh>
21 #include <dune/common/parallel/indexset.hh>
22 #include <dune/common/parallel/communication.hh>
23 #include <dune/istl/paamg/amg.hh>
24 #include <dune/istl/paamg/fastamg.hh>
25 #include <dune/istl/paamg/pinfo.hh>
26 #include <dune/istl/solvers.hh>
27 #include <cstdlib>
28 #include <ctime>
29 #include <pthread.h>
30 #define NUM_THREADS 3
31
32 typedef double XREAL;
33
34 /*
35 typedef HPA::xreal XREAL;
36
37 namespace Dune
38 {
39 template<>
40 struct DoubleConverter<HPA::xreal>
41 {
42 static double toDouble(const HPA::xreal& t)
43 {
44 return t._2double();
45 }
46 };
47 }
48 */
49
50 template<class M, class V>
51 void randomize(const M& mat, V& b)
52 {
53 V x=b;
54
55 srand((unsigned)std::clock());
56
57 typedef typename V::iterator iterator;
58 for(iterator i=x.begin(); i != x.end(); ++i)
59 *i=(rand() / (RAND_MAX + 1.0));
60
61 mat.mv(static_cast<const V&>(x), b);
62 }
63
64 typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet;
65 typedef Dune::FieldMatrix<XREAL,1,1> MatrixBlock;
66 typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat;
67 typedef Dune::FieldVector<XREAL,1> VectorBlock;
68 typedef Dune::BlockVector<VectorBlock> Vector;
69 typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator;
70 typedef Dune::Communication<void*> Comm;
71 typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> Smoother;
72 typedef Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs;
73
74 struct thread_arg
75 {
76 MYAMG *amg;
77 Vector *b;
78 Vector *x;
79 Operator *fop;
80 };
81
82
83 void *solve(void* arg)
84 {
85 thread_arg *amgarg=(thread_arg*) arg;
86
87 Dune::GeneralizedPCGSolver<Vector> amgCG(*amgarg->fop,*amgarg->amg,1e-6,80,2);
88 //Dune::LoopSolver<Vector> amgCG(fop, amg, 1e-4, 10000, 2);
89 Dune::Timer watch;
90 Dune::InverseOperatorResult r;
91 amgCG.apply(*amgarg->x,*amgarg->b,r);
92
93 double solvetime = watch.elapsed();
94
95 std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl;
96
97 return NULL;
98 }
99
100 void *solve2(void* arg)
101 {
102 thread_arg *amgarg=(thread_arg*) arg;
103 *amgarg->x=0;
104 (*amgarg->amg).pre(*amgarg->x,*amgarg->b);
105 (*amgarg->amg).apply(*amgarg->x,*amgarg->b);
106 (*amgarg->amg).post(*amgarg->x);
107 return 0;
108 }
109
110 template <int BS, typename AMG>
111 void testAMG(int N, int coarsenTarget, int ml)
112 {
113
114 std::cout<<"N="<<N<<" coarsenTarget="<<coarsenTarget<<" maxlevel="<<ml<<std::endl;
115
116
117
118 ParallelIndexSet indices;
119 int n;
120
121 Comm c;
122 BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, c, &n, 1);
123
124 Vector b(mat.N()), x(mat.M());
125
126 b=0;
127 x=100;
128
129 setBoundary(x, b, N);
130
131 x=0;
132 randomize(mat, b);
133
134 std::vector<Vector> xs(NUM_THREADS, x);
135 std::vector<Vector> bs(NUM_THREADS, b);
136
137 if(N<6) {
138 Dune::printmatrix(std::cout, mat, "A", "row");
139 Dune::printvector(std::cout, x, "x", "row");
140 }
141
142 Dune::Timer watch;
143
144 watch.reset();
145 Operator fop(mat);
146
147 typedef Dune::Amg::CoarsenCriterion<Dune::Amg::UnSymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> >
148 Criterion;
149 //typedef Dune::SeqSOR<BCRSMat,Vector,Vector> Smoother;
150 //typedef Dune::SeqJac<BCRSMat,Vector,Vector> Smoother;
151 //typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::MultiplicativeSchwarzMode> Smoother;
152 //typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::SymmetricMultiplicativeSchwarzMode> Smoother;
153 //typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector> Smoother;
154
155 SmootherArgs smootherArgs;
156
157 smootherArgs.iterations = 1;
158
159 //smootherArgs.overlap=SmootherArgs::vertex;
160 //smootherArgs.overlap=SmootherArgs::none;
161 //smootherArgs.overlap=SmootherArgs::aggregate;
162
163 smootherArgs.relaxationFactor = 1;
164
165 Criterion criterion(15,coarsenTarget);
166 criterion.setDefaultValuesIsotropic(2);
167 criterion.setAlpha(.67);
168 criterion.setBeta(1.0e-4);
169 criterion.setMaxLevel(ml);
170 criterion.setSkipIsolated(false);
171
172 Smoother smoother(mat,1,1);
173 AMG amg(fop, criterion);
174
175
176 double buildtime = watch.elapsed();
177
178 std::cout<<"Building hierarchy took "<<buildtime<<" seconds"<<std::endl;
179 std::vector<AMG> amgs(NUM_THREADS, amg);
180 std::vector<thread_arg> args(NUM_THREADS);
181 std::vector<pthread_t> threads(NUM_THREADS);
182 for(int i=0; i < NUM_THREADS; ++i)
183 {
184 args[i].amg=&amgs[i];
185 args[i].b=&bs[i];
186 args[i].x=&xs[i];
187 args[i].fop=&fop;
188 pthread_create(&threads[i], NULL, solve, (void*) &args[i]);
189 }
190 void* retval;
191
192 for(int i=0; i < NUM_THREADS; ++i)
193 pthread_join(threads[i], &retval);
194
195 amgs.clear();
196 args.clear();
197 amg.pre(x, b);
198 amgs.resize(NUM_THREADS, amg);
199 for(int i=0; i < NUM_THREADS; ++i)
200 {
201 args[i].amg=&amgs[i];
202 args[i].b=&bs[i];
203 args[i].x=&xs[i];
204 args[i].fop=&fop;
205 pthread_create(&threads[i], NULL, solve2, (void*) &args[i]);
206 }
207 for(int i=0; i < NUM_THREADS; ++i)
208 pthread_join(threads[i], &retval);
209
210 }
211
212
213 int main(int argc, char** argv)
214 try
215 {
216 int N=100;
217 int coarsenTarget=1200;
218 int ml=10;
219
220 if(argc>1)
221 N = atoi(argv[1]);
222
223 if(argc>2)
224 coarsenTarget = atoi(argv[2]);
225
226 if(argc>3)
227 ml = atoi(argv[3]);
228
229 testAMG<1,MYAMG>(N, coarsenTarget, ml);
230
231 //testAMG<2>(N, coarsenTarget, ml);
232
233 return 0;
234 }
235 catch (std::exception &e)
236 {
237 std::cout << "ERROR: " << e.what() << std::endl;
238 return 1;
239 }
240 catch (...)
241 {
242 std::cerr << "Dune reported an unknown error." << std::endl;
243 exit(1);
244 }