| 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 |
#include "anisotropic.hh" |
| 4 |
#include <dune/common/timer.hh> |
| 5 |
#include <dune/common/shared_ptr.hh> |
| 6 |
#include <dune/common/parallel/indexset.hh> |
| 7 |
#include <dune/common/parallel/communication.hh> |
| 8 |
#include <dune/istl/paamg/twolevelmethod.hh> |
| 9 |
#include <dune/istl/overlappingschwarz.hh> |
| 10 |
#include <dune/istl/paamg/pinfo.hh> |
| 11 |
#include <dune/istl/solvers.hh> |
| 12 |
|
| 13 |
#define NUM_THREADS 3 |
| 14 |
|
| 15 |
template<class M, class V> |
| 16 |
void randomize(const M& mat, V& b) |
| 17 |
{ |
| 18 |
V x=b; |
| 19 |
|
| 20 |
srand((unsigned)std::clock()); |
| 21 |
|
| 22 |
typedef typename V::iterator iterator; |
| 23 |
for(iterator i=x.begin(); i != x.end(); ++i) |
| 24 |
*i=(rand() / (RAND_MAX + 1.0)); |
| 25 |
|
| 26 |
mat.mv(static_cast<const V&>(x), b); |
| 27 |
} |
| 28 |
|
| 29 |
typedef double XREAL; |
| 30 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 31 |
typedef Dune::FieldMatrix<XREAL,1,1> MatrixBlock; |
| 32 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 33 |
typedef Dune::FieldVector<XREAL,1> VectorBlock; |
| 34 |
typedef Dune::BlockVector<VectorBlock> Vector; |
| 35 |
typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator; |
| 36 |
typedef Dune::Communication<void*> Comm; |
| 37 |
typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> Smoother; |
| 38 |
typedef Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs; |
| 39 |
#ifndef USE_OVERLAPPINGSCHWARZ |
| 40 |
typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> FSmoother; |
| 41 |
#else |
| 42 |
typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector, |
| 43 |
Dune::SymmetricMultiplicativeSchwarzMode> FSmoother; |
| 44 |
#endif |
| 45 |
typedef Dune::SeqJac<BCRSMat,Vector,Vector> CSmoother; |
| 46 |
typedef Dune::Amg::CoarsenCriterion< |
| 47 |
Dune::Amg::UnSymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> > |
| 48 |
Criterion; |
| 49 |
typedef Dune::Amg::OneStepAMGCoarseSolverPolicy<Operator,CSmoother, Criterion> |
| 50 |
CoarsePolicy; // Policy for coarse solver creation |
| 51 |
typedef Dune::Amg::TwoLevelMethod<Operator,CoarsePolicy,FSmoother> AMG; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
struct thread_arg |
| 56 |
{ |
| 57 |
AMG *amg; |
| 58 |
Vector *b; |
| 59 |
Vector *x; |
| 60 |
Operator *fop; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
void *solve(void* arg) |
| 65 |
{ |
| 66 |
thread_arg *amgarg=(thread_arg*) arg; |
| 67 |
|
| 68 |
Dune::GeneralizedPCGSolver<Vector> amgCG(*amgarg->fop,*amgarg->amg,1e-4,80,2); |
| 69 |
//Dune::LoopSolver<Vector> amgCG(fop, amg, 1e-4, 10000, 2); |
| 70 |
Dune::Timer watch; |
| 71 |
Dune::InverseOperatorResult r; |
| 72 |
amgCG.apply(*amgarg->x,*amgarg->b,r); |
| 73 |
|
| 74 |
double solvetime = watch.elapsed(); |
| 75 |
|
| 76 |
std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl; |
| 77 |
|
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
void *solve2(void* arg) |
| 82 |
{ |
| 83 |
thread_arg *amgarg=(thread_arg*) arg; |
| 84 |
*amgarg->x=0; |
| 85 |
(*amgarg->amg).pre(*amgarg->x,*amgarg->b); |
| 86 |
(*amgarg->amg).apply(*amgarg->x,*amgarg->b); |
| 87 |
(*amgarg->amg).post(*amgarg->x); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
void testTwoLevelMethod() |
| 92 |
{ |
| 93 |
const int BS=1; |
| 94 |
int N=100; |
| 95 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 96 |
ParallelIndexSet indices; |
| 97 |
typedef Dune::FieldMatrix<double,BS,BS> MatrixBlock; |
| 98 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 99 |
typedef Dune::FieldVector<double,BS> VectorBlock; |
| 100 |
typedef Dune::BlockVector<VectorBlock> Vector; |
| 101 |
typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator; |
| 102 |
typedef Dune::Communication<void*> Comm; |
| 103 |
Comm c; |
| 104 |
int n; |
| 105 |
BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, c, &n, 1); |
| 106 |
Vector b(mat.N()), x(mat.M()); |
| 107 |
x=0; |
| 108 |
randomize(mat, b); |
| 109 |
#ifndef USE_OVERLAPPINGSCHWARZ |
| 110 |
// Smoother used for the finest level |
| 111 |
FSmoother fineSmoother(mat,1,1.0); |
| 112 |
#else |
| 113 |
std::cout << "Use ovlps" << std::endl; |
| 114 |
// Smoother used for the finest level. There is an additional |
| 115 |
// template parameter to provide the subdomain solver. |
| 116 |
// Type of the subdomain vector |
| 117 |
typedef FSmoother::subdomain_vector SubdomainVector; |
| 118 |
// Create subdomain. |
| 119 |
std::size_t stride=2; |
| 120 |
SubdomainVector subdomains((((N-1)/stride)+1)*(((N-1)/stride)+1)); |
| 121 |
|
| 122 |
for(int i=0; i<N; ++i) |
| 123 |
for(int j=0; j<N; ++j) |
| 124 |
{ |
| 125 |
int index=i/stride*(((N-1)/stride)+1)+j/stride; |
| 126 |
subdomains[index].insert(i*N+j); |
| 127 |
} |
| 128 |
//create smoother |
| 129 |
FSmoother fineSmoother(mat,subdomains, 1.0, false); |
| 130 |
#endif |
| 131 |
// Create the approximate coarse level solver |
| 132 |
typedef Dune::SeqJac<BCRSMat,Vector,Vector> CSmoother; |
| 133 |
typedef Dune::Amg::CoarsenCriterion< |
| 134 |
Dune::Amg::UnSymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> > |
| 135 |
Criterion; |
| 136 |
typedef Dune::Amg::AggregationLevelTransferPolicy<Operator,Criterion> |
| 137 |
TransferPolicy; // Policy for coarse linear system creation |
| 138 |
typedef Dune::Amg::OneStepAMGCoarseSolverPolicy<Operator,CSmoother, Criterion> |
| 139 |
CoarsePolicy; // Policy for coarse solver creation |
| 140 |
typedef Dune::Amg::SmootherTraits<CSmoother>::Arguments SmootherArgs; |
| 141 |
Criterion crit; |
| 142 |
CoarsePolicy coarsePolicy=CoarsePolicy(SmootherArgs(), crit); |
| 143 |
TransferPolicy transferPolicy(crit); |
| 144 |
Operator fop(mat); |
| 145 |
Dune::Amg::TwoLevelMethod<Operator,CoarsePolicy,FSmoother> preconditioner(fop, |
| 146 |
Dune::stackobject_to_shared_ptr(fineSmoother), |
| 147 |
transferPolicy, |
| 148 |
coarsePolicy); |
| 149 |
Dune::GeneralizedPCGSolver<Vector> amgCG(fop,preconditioner,.8,80,2); |
| 150 |
Dune::Amg::TwoLevelMethod<Operator,CoarsePolicy,FSmoother> preconditioner1(preconditioner); |
| 151 |
Dune::InverseOperatorResult res; |
| 152 |
|
| 153 |
std::vector<AMG> amgs(NUM_THREADS, preconditioner1); |
| 154 |
std::vector<thread_arg> args(NUM_THREADS); |
| 155 |
std::vector<pthread_t> threads(NUM_THREADS); |
| 156 |
std::vector<Vector> xs(NUM_THREADS, x); |
| 157 |
std::vector<Vector> bs(NUM_THREADS, b); |
| 158 |
for(int i=0; i < NUM_THREADS; ++i) |
| 159 |
{ |
| 160 |
args[i].amg=&amgs[i]; |
| 161 |
args[i].b=&bs[i]; |
| 162 |
args[i].x=&xs[i]; |
| 163 |
args[i].fop=&fop; |
| 164 |
pthread_create(&threads[i], NULL, solve, (void*) &args[i]); |
| 165 |
} |
| 166 |
void* retval; |
| 167 |
|
| 168 |
for(int i=0; i < NUM_THREADS; ++i) |
| 169 |
pthread_join(threads[i], &retval); |
| 170 |
|
| 171 |
amgs.clear(); |
| 172 |
args.clear(); |
| 173 |
preconditioner.pre(x, b); |
| 174 |
amgs.resize(NUM_THREADS, preconditioner); |
| 175 |
for(int i=0; i < NUM_THREADS; ++i) |
| 176 |
{ |
| 177 |
args[i].amg=&amgs[i]; |
| 178 |
args[i].b=&bs[i]; |
| 179 |
args[i].x=&xs[i]; |
| 180 |
args[i].fop=&fop; |
| 181 |
pthread_create(&threads[i], NULL, solve2, (void*) &args[i]); |
| 182 |
} |
| 183 |
for(int i=0; i < NUM_THREADS; ++i) |
| 184 |
pthread_join(threads[i], &retval); |
| 185 |
|
| 186 |
// amgCG.apply(x,b,res); |
| 187 |
} |
| 188 |
|
| 189 |
int main() |
| 190 |
{ |
| 191 |
testTwoLevelMethod(); |
| 192 |
return 0; |
| 193 |
} |