20#pragma GCC diagnostic ignored "-Wdeprecated-copy"
21#pragma GCC diagnostic ignored "-Wint-in-bool-context"
26#include <boost/program_options.hpp>
27#include <boost/preprocessor/seq/for_each.hpp>
29#include "arccore/alina/PreconditionedSolver.h"
30#include "arccore/alina/make_block_solver.h"
31#include "arccore/alina/StaticMatrix.h"
32#include "arccore/alina/Adapters.h"
33#include "arccore/alina/AMG.h"
34#include "arccore/alina/SolverRuntime.h"
35#include "arccore/alina/CoarseningRuntime.h"
36#include "arccore/alina/RelaxationRuntime.h"
37#include "arccore/alina/SchurPressureCorrectionPreconditioner.h"
38#include "arccore/alina/PreconditionerRuntime.h"
39#include "arccore/alina/Adapters.h"
41#if defined(SOLVER_BACKEND_VEXCL)
43# ifndef SOLVER_BACKEND_BUILTIN
44# define SOLVER_BACKEND_BUILTIN
46#include "arccore/alina/BuiltinBackend.h"
47#ifdef BLOCK_TYPE_EIGEN
48#include "arccore/alina/ValueTypeEigen.h"
49template <
class T,
int N,
int M>
50using BlockMatrix = Eigen::Matrix<T, N, M>;
52 template <
class T,
int N,
int M>
58#include "arccore/alina/IO.h"
59#include "arccore/alina/Profiler.h"
61#ifndef ARCCORE_ALINA_BLOCK_SIZES
62# define ARCCORE_ALINA_BLOCK_SIZES (3)(4)
67using Alina::precondition;
70template <
class USolver,
class PSolver,
class Matrix>
73 auto& prof = Alina::Profiler::globalProfiler();
74 typedef Backend<double> SBackend;
75 SBackend::params bprm;
77 auto t1 = prof.scoped_tic(
"schur_complement");
85 std::cout << solve << std::endl;
88 auto f = SBackend::copy_vector(rhs, bprm);
89 auto x = SBackend::create_vector(rhs.size(), bprm);
90 Alina::backend::clear(*x);
96 std::cout <<
"Iterations: " << r.nbIteration() << std::endl
97 <<
"Error: " << r.residual() << std::endl;
100#define ARCCORE_ALINA_BLOCK_PSOLVER(z, data, B) \
102 typedef Backend<BlockMatrix<float, B, B>> BBackend; \
103 typedef ::Arcane::Alina::make_block_solver< \
104 ::Arcane::Alina::PreconditionerRuntime<BBackend>, \
105 ::Arcane::Alina::SolverRuntime<BBackend> > \
107 solve_schur<USolver, PSolver>(K, rhs, prm); \
111template <
class USolver,
class Matrix>
120 solve_schur<USolver, PSolver>(K, rhs, prm);
122#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
123 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_PSOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
126 precondition(
false,
"Unsupported block size for pressure");
130#define ARCCORE_ALINA_BLOCK_USOLVER(z, data, B) \
132 typedef Backend<BlockMatrix<float, B, B>> BBackend; \
133 typedef ::Arcane::Alina::make_block_solver< \
134 ::Arcane::Alina::PreconditionerRuntime<BBackend>, \
135 ::Arcane::Alina::SolverRuntime<BBackend>> \
137 solve_schur<USolver>(pb, K, rhs, prm); \
141template <
class Matrix>
148 solve_schur<USolver>(pb, K, rhs, prm);
150#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
151 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_USOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
154 precondition(
false,
"Unsupported block size for flow");
159int main(
int argc,
char* argv[])
161 auto& prof = Alina::Profiler::globalProfiler();
165 namespace po = boost::program_options;
166 namespace io = Alina::IO;
168 po::options_description desc(
"Options");
170 desc.add_options()(
"help,h",
"show help")(
172 po::bool_switch()->default_value(
false),
173 "When specified, treat input files as binary instead of as MatrixMarket. "
174 "It is assumed the files were converted to binary format with mm2bin utility. ")(
176 po::bool_switch()->default_value(
false),
177 "Scale the matrix so that the diagonal is unit. ")(
179 po::value<string>()->required(),
180 "The system matrix in MatrixMarket format")(
183 "The right-hand side in MatrixMarket format")(
186 "The pressure mask in MatrixMarket format. Or, if the parameter has "
187 "the form '%n:m', then each (n+i*m)-th variable is treated as pressure.")(
189 po::value<int>()->default_value(1),
190 "Block-size of the 'flow'/'non-pressure' part of the matrix")(
192 po::value<int>()->default_value(1),
193 "Block-size of the 'pressure' part of the matrix")(
196 "parameter file in json format")(
198 po::value<vector<string>>()->multitoken(),
199 "Parameters specified as name=value pairs. "
200 "May be provided multiple times. Examples:\n"
201 " -p solver.tol=1e-3\n"
202 " -p precond.coarse_enough=300");
204 po::variables_map vm;
205 po::store(po::parse_command_line(argc, argv, desc), vm);
207 if (vm.count(
"help")) {
208 std::cout << desc << std::endl;
215 if (vm.count(
"params"))
216 prm.read_json(vm[
"params"].as<string>());
218 if (vm.count(
"prm")) {
219 for (
const string& v : vm[
"prm"].as<vector<string>>()) {
225 vector<ptrdiff_t> ptr, col;
226 vector<double> val, rhs;
227 std::vector<char> pm;
230 auto t = prof.scoped_tic(
"reading");
232 string Afile = vm[
"matrix"].as<
string>();
233 bool binary = vm[
"binary"].as<
bool>();
236 io::read_crs(Afile, rows, ptr, col, val);
240 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
241 precondition(rows == cols,
"Non-square system matrix");
244 if (vm.count(
"rhs")) {
245 string bfile = vm[
"rhs"].as<
string>();
250 io::read_dense(bfile, n, m, rhs);
253 std::tie(n, m) = io::mm_reader(bfile)(rhs);
256 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
259 rhs.resize(rows, 1.0);
262 if (vm.count(
"pmask")) {
263 std::string pmask = vm[
"pmask"].as<
string>();
264 prm.put(
"precond.pmask_size", rows);
270 prm.put(
"precond.pmask_pattern", pmask);
275 precondition(n == rows && m == 1,
"Mask file has wrong size");
276 prm.put(
"precond.pmask",
static_cast<void*
>(&pm[0]));
282 if (vm[
"scale"].as<bool>()) {
283 std::vector<double> dia(rows, 1.0);
285 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
287 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
289 d = 1 /
sqrt(val[j]);
296 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
298 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
299 val[j] *= dia[i] * dia[col[j]];
304 solve_schur(vm[
"ub"].as<int>(), vm[
"pb"].as<int>(),
305 std::tie(rows, ptr, col, val), rhs, prm);
307 std::cout << prof << std::endl;
Convenience class that bundles together a preconditioner and an iterative solver.
Runtime-configurable preconditioners.
Matrix class, to be used by user.
__host__ __device__ double sqrt(double v)
Racine carrée de v.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Runtime-configurable wrappers around iterative solvers.