A minimal Rcpp wrapper for AMGCL: sparse CG and BiCGStab solvers with algebraic multigrid preconditioning, reusable solver objects, optional OpenMP, and an R-independent C++ interface.
Requires R 4.3.0 or later, Rcpp, Matrix,
and an R compiler toolchain (Rtools on Windows). Install from GitHub
with devtools:
install.packages("devtools") # If needed
devtools::install_github("santiagohermo/RcppAMGCL")Or build and install from a local checkout:
R CMD build .
R CMD INSTALL RcppAMGCL_0.1.0.tar.gzOpenMP is detected automatically. Set
RCPPAMGCL_OPENMP=on to require it or
RCPPAMGCL_OPENMP=off to build without it. Both builds
expose the same API.
library(RcppAMGCL)
A <- Matrix::bandSparse(1000, k = c(0, 1),
diagonals = list(rep(3, 1000), rep(-1, 999)), symmetric = TRUE)
b <- rep(1, nrow(A))
fit <- amgcl_solve(A, b)
fit$x
fit$converged
fit$residual
# Reuse setup for another right-hand side.
s <- amgcl_setup(A, solver = "cg", tol = 1e-8)
amgcl_solve(s, 2 * b)$xArguments:
A accepts dgCMatrix or
dsCMatrixb accepts a real numeric vector or one-column numeric
matrix.solver = "bicgstab" for nonsymmetric systems. CG
requires a symmetric positive definite matrix, which is not verified
automatically.Matrices must be square, finite, and have nonzero diagonal entries.
See ?amgcl_setup and ?amgcl_solve for the
complete input and lifetime contracts.
amgcl_info()
s <- amgcl_setup(A, threads = 2)
fit <- amgcl_solve(s, b, threads = 2)Each operation defaults to one thread.
Median seconds on an Intel Core Ultra 7 265H, Windows 11, R 4.5.3 and Matrix 1.7-4 with R’s supplied BLAS. Both methods reuse setup for ten distinct right-hand sides, with three repetitions per configuration.
| Poisson system | Method | Threads | Setup + 1 RHS (s) | Setup + 10 RHS (s) |
|---|---|---|---|---|
| 2D, 100,489 unknowns | AMGCL CG | 1 | 0.237 | 1.397 |
| 2D, 100,489 unknowns | AMGCL CG | 8 | 0.126 | 0.684 |
| 2D, 100,489 unknowns | Matrix Cholesky | 1 | 0.321 | 0.459 |
| 3D, 103,823 unknowns | AMGCL CG | 1 | 0.348 | 2.109 |
| 3D, 103,823 unknowns | AMGCL CG | 8 | 0.087 | 0.462 |
| 3D, 103,823 unknowns | Matrix Cholesky | 1 | 19.857 | 20.516 |
All solves met a relative residual target of 1e-8.
Cholesky achieved much tighter accuracy. AMGCL had cheaper setup in
these cases, while Cholesky was faster for ten RHS in 2D. Results depend
on the matrix, BLAS, hardware and accuracy target.
See benchmark methods and results for matrix sizes, setup and solve timings, totals, and reproduction commands.
Add RcppAMGCL to a downstream R package’s
LinkingTo field and include RcppAMGCL.h. The
header also works in standalone C++ without R or Rcpp:
#include <RcppAMGCL.h>
rcppamgcl::Matrix A;
A.n = 2;
A.ptr = {0, 2, 4}; // CSR row pointers
A.col = {0, 1, 0, 1}; // Sorted, zero-based column indices
A.val = {2, -1, -1, 2};
rcppamgcl::Solver solver(std::move(A), "cg", 1e-8, 1000, 1);
auto result = solver.solve(std::vector<double>{1, 0}, 1);The header is compatible with C++14. Downstream builds supply their own OpenMP compiler and linker flags. See the API and build contract.
AMGCL 1.4.5 is pinned and bundled with its MIT license. Boost is not required. The R wrapper is also MIT licensed.
See validation for executed checks and platform results.
This product includes Kratos Multi-Physics technology. See the bundled-code copyright inventory and third-party notices.
For publication references to this package and AMGCL, run
citation("RcppAMGCL") in R. The AMGCL reference is Demidov
(2020), AMGCL – A
C++ library for efficient solution of large sparse linear
systems.
The original AMGCL solver code was developed by Denis Demidov and upstream contributors in ddemidov/amgcl, not in this repository. RcppAMGCL brings that library to R through Rcpp, bundling AMGCL 1.4.5 with its original license notices and the compatibility patches documented in inst/AMGCL-VERSION.
GPT-6 Astra, working with delegated coding agents, implemented the RcppAMGCL wrapper and its R and C++ interfaces, build configuration, tests, documentation, and benchmark tooling. Fable 5.1 provided review and feedback on the package.