22#include <boost/range/iterator_range.hpp>
23#include <boost/preprocessor/seq/for_each.hpp>
25#include "arccore/alina/BuiltinBackend.h"
26#include "arccore/alina/ValueTypeComplex.h"
27#include "arccore/alina/StaticMatrix.h"
28#include "arccore/alina/Adapters.h"
30#include "arccore/alina/SolverRuntime.h"
31#include "arccore/alina/CoarseningRuntime.h"
32#include "arccore/alina/RelaxationRuntime.h"
33#include "arccore/alina/PreconditionerRuntime.h"
34#include "arccore/alina/PreconditionedSolver.h"
35#include "arccore/alina/AMG.h"
36#include "arccore/alina/IO.h"
38#include "arccore/alina/Profiler.h"
40#include "arccore/common/internal/ProgramOptions.h"
42#include "SampleProblemCommon.h"
44#ifndef ARCCORE_ALINA_BLOCK_SIZES
45#define ARCCORE_ALINA_BLOCK_SIZES (2)(3)(4)
48using namespace Arcane::Alina;
50using Alina::precondition;
53template <
class Precond,
class Matrix>
57 std::vector<std::complex<double>>
const& f,
58 std::vector<std::complex<double>>& x)
60 auto& prof = Alina::Profiler::globalProfiler();
62 typedef typename Precond::backend_type Backend;
64 typedef typename Alina::math::rhs_of<typename Backend::value_type>::type rhs_type;
65 size_t n = Alina::backend::nbRow(A);
67 rhs_type
const* fptr =
reinterpret_cast<rhs_type const*
>(&f[0]);
68 rhs_type* xptr =
reinterpret_cast<rhs_type*
>(&x[0]);
78 std::cout << solve << std::endl;
81 auto t = prof.scoped_tic(
"solve");
82 return solve(frng, xrng);
87int main(
int argc,
char* argv[])
89 auto& prof = Alina::Profiler::globalProfiler();
90 namespace po = Arcane::ProgramOptions;
91 namespace io = Alina::IO;
98 desc.add_options()(
"help,h",
"Show this help.")(
"prm-file,P",
100 "Parameter file in json format. ")(
102 po::value<vector<string>>()->multitoken(),
103 "Parameters specified as name=value pairs. "
104 "May be provided multiple times. Examples:\n"
105 " -p solver.tol=1e-3\n"
106 " -p precond.coarse_enough=300")(
"matrix,A",
108 "System matrix in the MatrixMarket format. "
109 "When not specified, solves a Poisson problem in 3D unit cube. ")(
112 "The RHS vector in the MatrixMarket format. "
113 "When omitted, a vector of ones is used by default. "
114 "Should only be provided together with a system matrix. ")(
117 "The near null-space vectors in the MatrixMarket format. "
118 "Should be a dense matrix of size N*M, where N is the number of "
119 "unknowns, and M is the number of null-space vectors. "
120 "Should only be provided together with a system matrix. ")(
122 po::bool_switch()->default_value(
false),
123 "When specified, treat input files as binary instead of as MatrixMarket. "
124 "It is assumed the files were converted to binary format with mm2bin utility. ")(
126 po::value<int>()->default_value(1),
127 "The block size of the system matrix. "
128 "When specified, the system matrix is assumed to have block-wise structure. "
129 "This usually is the case for problems in elasticity, structural mechanics, "
130 "for coupled systems of PDE (such as Navier-Stokes equations), etc. ")(
132 po::value<int>()->default_value(32),
133 "The size of the Poisson problem to solve when no system matrix is given. "
134 "Specified as number of grid nodes along each dimension of a unit cube. "
135 "The resulting system will have n*n*n unknowns. ")(
137 po::bool_switch()->default_value(
false),
138 "When specified, the AMG hierarchy is not constructed. "
139 "Instead, the problem is solved using a single-level smoother as preconditioner. ")(
141 po::value<double>()->default_value(0),
142 "Value to use as initial approximation. ")(
145 "Output file. Will be saved in the MatrixMarket format. "
146 "When omitted, the solution is not saved. ");
149 po::store(po::parse_command_line(argc, argv, desc), vm);
152 if (vm.count(
"help")) {
153 std::cout << desc << std::endl;
158 if (vm.count(
"prm-file")) {
159 prm.read_json(vm[
"prm-file"].as<string>());
162 if (vm.count(
"prm")) {
172 if (vm.count(
"matrix")) {
173 auto t = prof.scoped_tic(
"reading");
175 string Afile = vm[
"matrix"].as<
string>();
176 bool binary = vm[
"binary"].as<
bool>();
179 io::read_crs(Afile, rows, ptr, col, val);
183 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
184 precondition(rows == cols,
"Non-square system matrix");
187 if (vm.count(
"rhs")) {
188 string bfile = vm[
"rhs"].as<
string>();
192 io::read_dense(bfile, n, m, rhs);
195 std::tie(n, m) = io::mm_reader(bfile)(rhs);
198 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
201 rhs.resize(rows, 1.0);
204 if (vm.count(
"null")) {
205 string nfile = vm[
"null"].as<
string>();
210 io::read_dense(nfile, m, nv, null);
213 std::tie(m, nv) = io::mm_reader(nfile)(null);
216 precondition(m == rows,
"Near null-space vectors have wrong size");
218 prm.put(
"precond.coarsening.nullspace.cols", nv);
219 prm.put(
"precond.coarsening.nullspace.rows", rows);
220 prm.put(
"precond.coarsening.nullspace.B", &null[0]);
224 auto t = prof.scoped_tic(
"assembling");
225 rows = sample_problem(vm[
"size"].as<int>(), val, col, ptr, rhs);
228 x.resize(rows, vm[
"initial"].as<double>());
230 if (vm[
"single-level"].as<bool>())
231 prm.put(
"precond.class",
"relaxation");
233 int block_size = vm[
"block-size"].as<
int>();
235#define CALL_BLOCK_SOLVER(z, data, B) \
237 typedef StaticMatrix<std::complex<double>, B, B> value_type; \
238 typedef ::Arcane::Alina::BuiltinBackend<value_type> Backend; \
239 r = solve<::Arcane::Alina::PreconditionerRuntime<Backend>>( \
240 ::Arcane::Alina::adapter::block_matrix<value_type>( \
241 std::tie(rows, ptr, col, val)), \
245 switch (block_size) {
248 r = solve<PreconditionerRuntime<Backend>>(
249 std::tie(rows, ptr, col, val), prm, rhs, x);
251 BOOST_PP_SEQ_FOR_EACH(CALL_BLOCK_SOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
254#undef CALL_BLOCK_SOLVER
256 if (vm.count(
"output")) {
257 auto t = prof.scoped_tic(
"write");
258 Alina::IO::mm_write(vm[
"output"].as<string>(), &x[0], x.size());
261 std::cout <<
"Iterations: " << r.nbIteration() << std::endl
262 <<
"Error: " << r.residual() << std::endl
263 << prof << std::endl;
Convenience class that bundles together a preconditioner and an iterative solver.
Matrix class, to be used by user.
Describes a set of command-line options.
Stores parsed option values.
View of an array of elements of type T.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --