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>
58 std::vector<std::complex<double>>
const& f,
59 std::vector<std::complex<double>>& x)
61 auto& prof = Alina::Profiler::globalProfiler();
63 typedef typename Precond::backend_type Backend;
65 typedef typename Alina::math::rhs_of<typename Backend::value_type>::type rhs_type;
66 size_t n = Alina::backend::nbRow(A);
68 rhs_type
const* fptr =
reinterpret_cast<rhs_type const*
>(&f[0]);
69 rhs_type* xptr =
reinterpret_cast<rhs_type*
>(&x[0]);
76 Solver solve(context, A, prm);
79 std::cout << solve << std::endl;
82 auto t = prof.scoped_tic(
"solve");
83 return solve(frng, xrng);
88int main(
int argc,
char* argv[])
90 auto& prof = Alina::Profiler::globalProfiler();
91 namespace po = Arcane::ProgramOptions;
92 namespace io = Alina::IO;
99 desc.add_options()(
"help,h",
"Show this help.");
100 desc.add_options()(
"prm-file,P",
102 "Parameter file in json format. ");
103 desc.add_options()(
"prm,p",
104 po::value<vector<string>>()->multitoken(),
105 "Parameters specified as name=value pairs. "
106 "May be provided multiple times. Examples:\n"
107 " -p solver.tol=1e-3\n"
108 " -p precond.coarse_enough=300");
109 desc.add_options()(
"matrix,A",
111 "System matrix in the MatrixMarket format. "
112 "When not specified, solves a Poisson problem in 3D unit cube. ");
113 desc.add_options()(
"rhs,f",
115 "The RHS vector in the MatrixMarket format. "
116 "When omitted, a vector of ones is used by default. "
117 "Should only be provided together with a system matrix. ");
118 desc.add_options()(
"null,N",
120 "The near null-space vectors in the MatrixMarket format. "
121 "Should be a dense matrix of size N*M, where N is the number of "
122 "unknowns, and M is the number of null-space vectors. "
123 "Should only be provided together with a system matrix. ");
124 desc.add_options()(
"binary,B",
125 po::bool_switch()->default_value(
false),
126 "When specified, treat input files as binary instead of as MatrixMarket. "
127 "It is assumed the files were converted to binary format with mm2bin utility. ");
128 desc.add_options()(
"block-size,b",
129 po::value<int>()->default_value(1),
130 "The block size of the system matrix. "
131 "When specified, the system matrix is assumed to have block-wise structure. "
132 "This usually is the case for problems in elasticity, structural mechanics, "
133 "for coupled systems of PDE (such as Navier-Stokes equations), etc. ");
134 desc.add_options()(
"size,n",
135 po::value<int>()->default_value(32),
136 "The size of the Poisson problem to solve when no system matrix is given. "
137 "Specified as number of grid nodes along each dimension of a unit cube. "
138 "The resulting system will have n*n*n unknowns. ");
139 desc.add_options()(
"single-level,1",
140 po::bool_switch()->default_value(
false),
141 "When specified, the AMG hierarchy is not constructed. "
142 "Instead, the problem is solved using a single-level smoother as preconditioner. ");
143 desc.add_options()(
"initial,x",
144 po::value<double>()->default_value(0),
145 "Value to use as initial approximation. ");
146 desc.add_options()(
"output,o",
148 "Output file. Will be saved in the MatrixMarket format. "
149 "When omitted, the solution is not saved. ");
152 po::store(po::parse_command_line(argc, argv, desc), vm);
155 if (vm.count(
"help")) {
156 std::cout << desc << std::endl;
161 if (vm.count(
"prm-file")) {
162 prm.read_json(vm[
"prm-file"].as<string>());
165 if (vm.count(
"prm")) {
175 if (vm.count(
"matrix")) {
176 auto t = prof.scoped_tic(
"reading");
178 string Afile = vm[
"matrix"].as<
string>();
179 bool binary = vm[
"binary"].as<
bool>();
182 io::read_crs(Afile, rows, ptr, col, val);
186 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
187 precondition(rows == cols,
"Non-square system matrix");
190 if (vm.count(
"rhs")) {
191 string bfile = vm[
"rhs"].as<
string>();
195 io::read_dense(bfile, n, m, rhs);
198 std::tie(n, m) = io::mm_reader(bfile)(rhs);
201 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
204 rhs.resize(rows, 1.0);
207 if (vm.count(
"null")) {
208 string nfile = vm[
"null"].as<
string>();
213 io::read_dense(nfile, m, nv, null);
216 std::tie(m, nv) = io::mm_reader(nfile)(null);
219 precondition(m == rows,
"Near null-space vectors have wrong size");
221 prm.put(
"precond.coarsening.nullspace.cols", nv);
222 prm.put(
"precond.coarsening.nullspace.rows", rows);
223 prm.put(
"precond.coarsening.nullspace.B", &null[0]);
227 auto t = prof.scoped_tic(
"assembling");
228 rows = Alina::PoissonProblemGenerator::createSequentialMatrix(vm[
"size"].as<int>(), val, col, ptr, rhs);
231 x.resize(rows, vm[
"initial"].as<double>());
233 if (vm[
"single-level"].as<bool>())
234 prm.put(
"precond.class",
"relaxation");
236 int block_size = vm[
"block-size"].as<
int>();
238#define CALL_BLOCK_SOLVER(z, data, B) \
240 typedef StaticMatrix<std::complex<double>, B, B> value_type; \
241 typedef ::Arcane::Alina::BuiltinBackend<value_type> Backend; \
242 r = solve<::Arcane::Alina::PreconditionerRuntime<Backend>>(context, \
243 ::Arcane::Alina::adapter::block_matrix<value_type>( \
244 std::tie(rows, ptr, col, val)), \
248 switch (block_size) {
251 r = solve<PreconditionerRuntime<Backend>>(context, std::tie(rows, ptr, col, val), prm, rhs, x);
253 BOOST_PP_SEQ_FOR_EACH(CALL_BLOCK_SOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
256#undef CALL_BLOCK_SOLVER
258 if (vm.count(
"output")) {
259 auto t = prof.scoped_tic(
"write");
260 Alina::IO::mm_write(vm[
"output"].as<string>(), &x[0], x.size());
263 std::cout <<
"Iterations: " << r.nbIteration() << std::endl
264 <<
"Error: " << r.residual() << std::endl
265 << prof << std::endl;
Convenience class that bundles together a preconditioner and an iterative solver.
Class to store parameters as a hierarchical key/value tree.
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 --