Created
October 5, 2020 17:27
-
-
Save ddemidov/e086eb1051eb1e5aab0d96f2ee25bc58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <Eigen/Sparse> | |
#include <Eigen/Dense> | |
#include <unsupported/Eigen/SparseExtra> // For reading MatrixMarket files | |
#include <amgcl/adapter/eigen.hpp> | |
#include <amgcl/adapter/block_matrix.hpp> | |
#include <amgcl/value_type/static_matrix.hpp> | |
#include <amgcl/make_solver.hpp> | |
#include <amgcl/solver/bicgstab.hpp> | |
#include <amgcl/amg.hpp> | |
#include <amgcl/coarsening/smoothed_aggregation.hpp> | |
#include <amgcl/relaxation/spai0.hpp> | |
#include <amgcl/profiler.hpp> | |
AMGCL_USE_EIGEN_VECTORS_WITH_BUILTIN_BACKEND() | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
std::cerr << "Usage: " << argv[0] << " <matrix.mm>" << std::endl; | |
return 1; | |
} | |
amgcl::profiler<> prof; | |
// Read sparse matrix from MatrixMarket format. | |
// In general this should come pre-assembled. | |
Eigen::SparseMatrix<double, Eigen::RowMajor> A; | |
prof.tic("read"); | |
Eigen::loadMarket(A, argv[1]); | |
prof.toc("read"); | |
// Use vector of ones as RHS for simplicity: | |
Eigen::VectorXd f = Eigen::VectorXd::Constant(A.rows(), 1.0); | |
// Zero initial approximation: | |
Eigen::VectorXd x = Eigen::VectorXd::Zero(A.rows()); | |
typedef amgcl::static_matrix<double, 2, 2> val_type; | |
typedef amgcl::static_matrix<double, 2, 1> rhs_type; | |
// Setup the solver: | |
typedef amgcl::make_solver< | |
amgcl::amg< | |
amgcl::backend::builtin<val_type>, | |
amgcl::coarsening::smoothed_aggregation, | |
amgcl::relaxation::spai0 | |
>, | |
amgcl::solver::bicgstab<amgcl::backend::builtin<val_type> > | |
> Solver; | |
prof.tic("setup"); | |
Solver solve(amgcl::adapter::block_matrix<val_type>(A)); | |
prof.toc("setup"); | |
std::cout << solve << std::endl; | |
// Solve the system for the given RHS: | |
rhs_type * fptr = reinterpret_cast<rhs_type*>(&f[0]); | |
rhs_type * xptr = reinterpret_cast<rhs_type*>(&x[0]); | |
auto frng = amgcl::make_iterator_range(fptr, fptr + A.rows() / 2); | |
auto xrng = amgcl::make_iterator_range(xptr, xptr + A.rows() / 2); | |
prof.tic("solve"); | |
int iters; | |
double error; | |
std::tie(iters, error) = solve(frng, xrng); | |
prof.toc("solve"); | |
std::cout << iters << " " << error << std::endl | |
<< prof << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment