// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception #include "anisotropic.hh" #include #include #include #include #include #include #include #include #define NUM_THREADS 3 template void randomize(const M& mat, V& b) { V x=b; srand((unsigned)std::clock()); typedef typename V::iterator iterator; for(iterator i=x.begin(); i != x.end(); ++i) *i=(rand() / (RAND_MAX + 1.0)); mat.mv(static_cast(x), b); } typedef double XREAL; typedef Dune::ParallelIndexSet ParallelIndexSet; typedef Dune::FieldMatrix MatrixBlock; typedef Dune::BCRSMatrix BCRSMat; typedef Dune::FieldVector VectorBlock; typedef Dune::BlockVector Vector; typedef Dune::MatrixAdapter Operator; typedef Dune::Communication Comm; typedef Dune::SeqSSOR Smoother; typedef Dune::Amg::SmootherTraits::Arguments SmootherArgs; #ifndef USE_OVERLAPPINGSCHWARZ typedef Dune::SeqSSOR FSmoother; #else typedef Dune::SeqOverlappingSchwarz FSmoother; #endif typedef Dune::SeqJac CSmoother; typedef Dune::Amg::CoarsenCriterion< Dune::Amg::UnSymmetricCriterion > Criterion; typedef Dune::Amg::OneStepAMGCoarseSolverPolicy CoarsePolicy; // Policy for coarse solver creation typedef Dune::Amg::TwoLevelMethod AMG; struct thread_arg { AMG *amg; Vector *b; Vector *x; Operator *fop; }; void *solve(void* arg) { thread_arg *amgarg=(thread_arg*) arg; Dune::GeneralizedPCGSolver amgCG(*amgarg->fop,*amgarg->amg,1e-4,80,2); //Dune::LoopSolver amgCG(fop, amg, 1e-4, 10000, 2); Dune::Timer watch; Dune::InverseOperatorResult r; amgCG.apply(*amgarg->x,*amgarg->b,r); double solvetime = watch.elapsed(); std::cout<<"AMG solving took "<x=0; (*amgarg->amg).pre(*amgarg->x,*amgarg->b); (*amgarg->amg).apply(*amgarg->x,*amgarg->b); (*amgarg->amg).post(*amgarg->x); return 0; } void testTwoLevelMethod() { const int BS=1; int N=100; typedef Dune::ParallelIndexSet ParallelIndexSet; ParallelIndexSet indices; typedef Dune::FieldMatrix MatrixBlock; typedef Dune::BCRSMatrix BCRSMat; typedef Dune::FieldVector VectorBlock; typedef Dune::BlockVector Vector; typedef Dune::MatrixAdapter Operator; typedef Dune::Communication Comm; Comm c; int n; BCRSMat mat = setupAnisotropic2d(N, indices, c, &n, 1); Vector b(mat.N()), x(mat.M()); x=0; randomize(mat, b); #ifndef USE_OVERLAPPINGSCHWARZ // Smoother used for the finest level FSmoother fineSmoother(mat,1,1.0); #else std::cout << "Use ovlps" << std::endl; // Smoother used for the finest level. There is an additional // template parameter to provide the subdomain solver. // Type of the subdomain vector typedef FSmoother::subdomain_vector SubdomainVector; // Create subdomain. std::size_t stride=2; SubdomainVector subdomains((((N-1)/stride)+1)*(((N-1)/stride)+1)); for(int i=0; i CSmoother; typedef Dune::Amg::CoarsenCriterion< Dune::Amg::UnSymmetricCriterion > Criterion; typedef Dune::Amg::AggregationLevelTransferPolicy TransferPolicy; // Policy for coarse linear system creation typedef Dune::Amg::OneStepAMGCoarseSolverPolicy CoarsePolicy; // Policy for coarse solver creation typedef Dune::Amg::SmootherTraits::Arguments SmootherArgs; Criterion crit; CoarsePolicy coarsePolicy=CoarsePolicy(SmootherArgs(), crit); TransferPolicy transferPolicy(crit); Operator fop(mat); Dune::Amg::TwoLevelMethod preconditioner(fop, Dune::stackobject_to_shared_ptr(fineSmoother), transferPolicy, coarsePolicy); Dune::GeneralizedPCGSolver amgCG(fop,preconditioner,.8,80,2); Dune::Amg::TwoLevelMethod preconditioner1(preconditioner); Dune::InverseOperatorResult res; std::vector amgs(NUM_THREADS, preconditioner1); std::vector args(NUM_THREADS); std::vector threads(NUM_THREADS); std::vector xs(NUM_THREADS, x); std::vector bs(NUM_THREADS, b); for(int i=0; i < NUM_THREADS; ++i) { args[i].amg=&amgs[i]; args[i].b=&bs[i]; args[i].x=&xs[i]; args[i].fop=&fop; pthread_create(&threads[i], NULL, solve, (void*) &args[i]); } void* retval; for(int i=0; i < NUM_THREADS; ++i) pthread_join(threads[i], &retval); amgs.clear(); args.clear(); preconditioner.pre(x, b); amgs.resize(NUM_THREADS, preconditioner); for(int i=0; i < NUM_THREADS; ++i) { args[i].amg=&amgs[i]; args[i].b=&bs[i]; args[i].x=&xs[i]; args[i].fop=&fop; pthread_create(&threads[i], NULL, solve2, (void*) &args[i]); } for(int i=0; i < NUM_THREADS; ++i) pthread_join(threads[i], &retval); // amgCG.apply(x,b,res); } int main() { testTwoLevelMethod(); return 0; }