22#include <boost/program_options.hpp>
24#include "arccore/alina/BuiltinBackend.h"
25#include "arccore/alina/RelaxationRuntime.h"
26#include "arccore/alina/CoarseningRuntime.h"
27#include "arccore/alina/SolverRuntime.h"
28#include "arccore/alina/PreconditionerRuntime.h"
29#include "arccore/alina/DeflatedSolver.h"
30#include "arccore/alina/AMG.h"
31#include "arccore/alina/Adapters.h"
32#include "arccore/alina/IO.h"
34#include "arccore/alina/Profiler.h"
38using Alina::precondition;
41int main(
int argc,
char* argv[])
43 auto& prof = Alina::Profiler::globalProfiler();
45 namespace po = boost::program_options;
46 namespace io = Alina::IO;
51 po::options_description desc(
"Options");
53 desc.add_options()(
"help,h",
"Show this help.")(
"prm-file,P",
55 "Parameter file in json format. ")(
57 po::value<vector<string>>()->multitoken(),
58 "Parameters specified as name=value pairs. "
59 "May be provided multiple times. Examples:\n"
60 " -p solver.tol=1e-3\n"
61 " -p precond.coarse_enough=300")(
"matrix,A",
62 po::value<string>()->required(),
63 "System matrix in the MatrixMarket format.")(
66 "The RHS vector in the MatrixMarket format. "
67 "When omitted, a vector of ones is used by default. "
68 "Should only be provided together with a system matrix. ")(
71 "The near null-space vectors in the MatrixMarket format. ")(
74 "Coordinate matrix where number of rows corresponds to the number of grid nodes "
75 "and the number of columns corresponds to the problem dimensionality (2 or 3). "
76 "Will be used to construct near null-space vectors as rigid body modes. ")(
78 po::bool_switch()->default_value(
false),
79 "When specified, treat input files as binary instead of as MatrixMarket. "
80 "It is assumed the files were converted to binary format with mm2bin utility. ")(
82 po::bool_switch()->default_value(
false),
83 "When specified, the AMG hierarchy is not constructed. "
84 "Instead, the problem is solved using a single-level smoother as preconditioner. ")(
87 "Output file. Will be saved in the MatrixMarket format. "
88 "When omitted, the solution is not saved. ");
90 po::positional_options_description p;
94 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
97 if (vm.count(
"help")) {
98 std::cout << desc << std::endl;
102 for (
int i = 0; i < argc; ++i) {
105 std::cout << argv[i];
107 std::cout << std::endl;
110 if (vm.count(
"prm-file")) {
111 prm.read_json(vm[
"prm-file"].as<string>());
114 if (vm.count(
"prm")) {
115 for (
const string& v : vm[
"prm"].as<vector<string>>()) {
120 if (!vm.count(
"defvec") && !vm.count(
"coords")) {
121 std::cerr <<
"Either defvec or coords should be given" << std::endl;
126 vector<ptrdiff_t> ptr, col;
127 vector<double> val, rhs, z;
130 auto t = prof.scoped_tic(
"reading");
132 string Afile = vm[
"matrix"].as<
string>();
133 bool binary = vm[
"binary"].as<
bool>();
136 io::read_crs(Afile, rows, ptr, col, val);
140 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
141 precondition(rows == cols,
"Non-square system matrix");
144 if (vm.count(
"rhs")) {
145 string bfile = vm[
"rhs"].as<
string>();
150 io::read_dense(bfile, n, m, rhs);
153 std::tie(n, m) = io::mm_reader(bfile)(rhs);
156 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
159 rhs.resize(rows, 1.0);
162 if (vm.count(
"defvec")) {
163 string nfile = vm[
"defvec"].as<
string>();
164 std::vector<double> N;
169 io::read_dense(nfile, m, nv, N);
172 std::tie(m, nv) = io::mm_reader(nfile)(N);
175 precondition(m == rows,
"Deflation vectors have wrong size");
178 for (ptrdiff_t i = 0; i < rows; ++i)
179 for (ptrdiff_t j = 0; j < nv; ++j)
180 z[i + j * rows] = N[i * nv + j];
182 else if (vm.count(
"coords")) {
183 string cfile = vm[
"coords"].as<
string>();
184 std::vector<double> coo;
189 io::read_dense(cfile, m, ndim, coo);
192 std::tie(m, ndim) = io::mm_reader(cfile)(coo);
195 precondition(m * ndim == rows && (ndim == 2 || ndim == 3),
"Coordinate matrix has wrong size");
197 nv = Alina::rigid_body_modes(ndim, coo, z,
true);
201 prm.put(
"vec", z.data());
204 std::vector<double> x(rows, 0);
206 if (vm[
"single-level"].as<bool>())
207 prm.put(
"precond.class",
"relaxation");
214 auto A = std::tie(rows, ptr, col, val);
217 Solver solve(A, prm);
224 if (vm.count(
"output")) {
225 auto t = prof.scoped_tic(
"write");
226 Alina::IO::mm_write(vm[
"output"].as<string>(), x.data(), x.size());
229 std::vector<double> r(rows);
230 Alina::backend::residual(rhs, A, x, r);
232 std::cout <<
"Iterations: " << result.nbIteration() << std::endl
233 <<
"Error: " << result.residual() << std::endl
234 <<
"True error: " <<
sqrt(Alina::backend::inner_product(r, r)) /
sqrt(Alina::backend::inner_product(rhs, rhs))
235 << prof << std::endl;
Iterative preconditioned solver with deflation.
__host__ __device__ double sqrt(double v)
Racine carrée de v.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Runtime-configurable wrappers around iterative solvers.