22#include "arccore/alina/BuiltinBackend.h"
23#include "arccore/alina/RelaxationRuntime.h"
24#include "arccore/alina/CoarseningRuntime.h"
25#include "arccore/alina/SolverRuntime.h"
26#include "arccore/alina/PreconditionerRuntime.h"
27#include "arccore/alina/DeflatedSolver.h"
28#include "arccore/alina/AMG.h"
29#include "arccore/alina/Adapters.h"
30#include "arccore/alina/IO.h"
31#include "arccore/alina/Profiler.h"
33#include "arccore/common/internal/ProgramOptions.h"
37using Alina::precondition;
40int main(
int argc,
char* argv[])
42 auto& prof = Alina::Profiler::globalProfiler();
44 namespace po = Arcane::ProgramOptions;
45 namespace io = Alina::IO;
52 desc.add_options()(
"help,h",
"Show this help.")(
"prm-file,P",
54 "Parameter file in json format. ")(
56 po::value<vector<string>>()->multitoken(),
57 "Parameters specified as name=value pairs. "
58 "May be provided multiple times. Examples:\n"
59 " -p solver.tol=1e-3\n"
60 " -p precond.coarse_enough=300")(
"matrix,A",
61 po::value<string>()->required(),
62 "System matrix in the MatrixMarket format.")(
65 "The RHS vector in the MatrixMarket format. "
66 "When omitted, a vector of ones is used by default. "
67 "Should only be provided together with a system matrix. ")(
70 "The near null-space vectors in the MatrixMarket format. ")(
73 "Coordinate matrix where number of rows corresponds to the number of grid nodes "
74 "and the number of columns corresponds to the problem dimensionality (2 or 3). "
75 "Will be used to construct near null-space vectors as rigid body modes. ")(
77 po::bool_switch()->default_value(
false),
78 "When specified, treat input files as binary instead of as MatrixMarket. "
79 "It is assumed the files were converted to binary format with mm2bin utility. ")(
81 po::bool_switch()->default_value(
false),
82 "When specified, the AMG hierarchy is not constructed. "
83 "Instead, the problem is solved using a single-level smoother as preconditioner. ")(
86 "Output file. Will be saved in the MatrixMarket format. "
87 "When omitted, the solution is not saved. ");
96 if (vm.count(
"help")) {
97 std::cout << desc << std::endl;
101 for (
int i = 0; i < argc; ++i) {
104 std::cout << argv[i];
106 std::cout << std::endl;
109 if (vm.count(
"prm-file")) {
110 prm.read_json(vm[
"prm-file"].as<string>());
113 if (vm.count(
"prm")) {
114 for (
const string& v : vm[
"prm"].as<vector<string>>()) {
119 if (!vm.count(
"defvec") && !vm.count(
"coords")) {
120 std::cerr <<
"Either defvec or coords should be given" << std::endl;
125 vector<ptrdiff_t> ptr, col;
126 vector<double> val, rhs, z;
129 auto t = prof.scoped_tic(
"reading");
131 string Afile = vm[
"matrix"].as<
string>();
132 bool binary = vm[
"binary"].as<
bool>();
135 io::read_crs(Afile, rows, ptr, col, val);
139 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
140 precondition(rows == cols,
"Non-square system matrix");
143 if (vm.count(
"rhs")) {
144 string bfile = vm[
"rhs"].as<
string>();
149 io::read_dense(bfile, n, m, rhs);
152 std::tie(n, m) = io::mm_reader(bfile)(rhs);
155 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
158 rhs.resize(rows, 1.0);
161 if (vm.count(
"defvec")) {
162 string nfile = vm[
"defvec"].as<
string>();
163 std::vector<double> N;
168 io::read_dense(nfile, m, nv, N);
171 std::tie(m, nv) = io::mm_reader(nfile)(N);
174 precondition(m == rows,
"Deflation vectors have wrong size");
177 for (ptrdiff_t i = 0; i < rows; ++i)
178 for (ptrdiff_t j = 0; j < nv; ++j)
179 z[i + j * rows] = N[i * nv + j];
181 else if (vm.count(
"coords")) {
182 string cfile = vm[
"coords"].as<
string>();
183 std::vector<double> coo;
188 io::read_dense(cfile, m, ndim, coo);
191 std::tie(m, ndim) = io::mm_reader(cfile)(coo);
194 precondition(m * ndim == rows && (ndim == 2 || ndim == 3),
"Coordinate matrix has wrong size");
196 nv = Alina::rigid_body_modes(ndim, coo, z,
true);
200 prm.put(
"vec", z.data());
203 std::vector<double> x(rows, 0);
205 if (vm[
"single-level"].as<bool>())
206 prm.put(
"precond.class",
"relaxation");
213 auto A = std::tie(rows, ptr, col, val);
216 Solver solve(A, prm);
223 if (vm.count(
"output")) {
224 auto t = prof.scoped_tic(
"write");
225 Alina::IO::mm_write(vm[
"output"].as<string>(), x.data(), x.size());
228 std::vector<double> r(rows);
229 Alina::backend::residual(rhs, A, x, r);
231 std::cout <<
"Iterations: " << result.nbIteration() << std::endl
232 <<
"Error: " << result.residual() << std::endl
233 <<
"True error: " <<
sqrt(Alina::backend::inner_product(r, r)) /
sqrt(Alina::backend::inner_product(rhs, rhs))
234 << prof << std::endl;
Iterative preconditioned solver with deflation.
Fluent command-line parser builder.
Describes a set of command-line options.
Describes positional (non-option) arguments.
Stores parsed option values.
apfloat sqrt(apfloat v)
Square root of v.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Runtime-configurable wrappers around iterative solvers.