Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
SCHURPreconditionerMixed.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/*---------------------------------------------------------------------------*/
9/*
10 * This file is based on the work on AMGCL library (version march 2026)
11 * which can be found at https://github.com/ddemidov/amgcl.
12 *
13 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
14 * SPDX-License-Identifier: MIT
15 */
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18
19// Pour Eigen
20#pragma GCC diagnostic ignored "-Wdeprecated-copy"
21#pragma GCC diagnostic ignored "-Wint-in-bool-context"
22
23#include <iostream>
24#include <string>
25
26#include <boost/program_options.hpp>
27#include <boost/preprocessor/seq/for_each.hpp>
28
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"
40
41#if defined(SOLVER_BACKEND_VEXCL)
42#else
43# ifndef SOLVER_BACKEND_BUILTIN
44# define SOLVER_BACKEND_BUILTIN
45# endif
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>;
51# else
52 template <class T, int N, int M>
53 using BlockMatrix = Arcane::Alina::StaticMatrix<T, N, M>;
54#endif
55template <class T> using Backend = Arcane::Alina::BuiltinBackend<T>;
56#endif
57
58#include "arccore/alina/IO.h"
59#include "arccore/alina/Profiler.h"
60
61#ifndef ARCCORE_ALINA_BLOCK_SIZES
62# define ARCCORE_ALINA_BLOCK_SIZES (3)(4)
63#endif
64
65using namespace Arcane;
66
67using Alina::precondition;
68
69//---------------------------------------------------------------------------
70template <class USolver, class PSolver, class Matrix>
71void solve_schur(const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
72{
73 auto& prof = Alina::Profiler::globalProfiler();
74 typedef Backend<double> SBackend;
75 SBackend::params bprm;
76
77 auto t1 = prof.scoped_tic("schur_complement");
78
79 prof.tic("setup");
82 solve(K, prm, bprm);
83 prof.toc("setup");
84
85 std::cout << solve << std::endl;
86
87 auto A = SBackend::copy_matrix(std::make_shared<Alina::CSRMatrix<double>>(K), bprm);
88 auto f = SBackend::copy_vector(rhs, bprm);
89 auto x = SBackend::create_vector(rhs.size(), bprm);
90 Alina::backend::clear(*x);
91
92 prof.tic("solve");
93 Alina::SolverResult r = solve(*A, *f, *x);
94 prof.toc("solve");
95
96 std::cout << "Iterations: " << r.nbIteration() << std::endl
97 << "Error: " << r.residual() << std::endl;
98}
99
100#define ARCCORE_ALINA_BLOCK_PSOLVER(z, data, B) \
101 case 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> > \
106 PSolver; \
107 solve_schur<USolver, PSolver>(K, rhs, prm); \
108 } break;
109
110//---------------------------------------------------------------------------
111template <class USolver, class Matrix>
112void solve_schur(int pb, const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
113{
114 switch (pb) {
115 case 1: {
119 PSolver;
120 solve_schur<USolver, PSolver>(K, rhs, prm);
121 } break;
122#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
123 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_PSOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
124#endif
125 default:
126 precondition(false, "Unsupported block size for pressure");
127 }
128}
129
130#define ARCCORE_ALINA_BLOCK_USOLVER(z, data, B) \
131 case 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>> \
136 USolver; \
137 solve_schur<USolver>(pb, K, rhs, prm); \
138 } break;
139
140//---------------------------------------------------------------------------
141template <class Matrix>
142void solve_schur(int ub, int pb, const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
143{
144 switch (ub) {
145 case 1: {
148 solve_schur<USolver>(pb, K, rhs, prm);
149 } break;
150#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
151 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_USOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
152#endif
153 default:
154 precondition(false, "Unsupported block size for flow");
155 }
156}
157
158//---------------------------------------------------------------------------
159int main(int argc, char* argv[])
160{
161 auto& prof = Alina::Profiler::globalProfiler();
162 using std::string;
163 using std::vector;
164
165 namespace po = boost::program_options;
166 namespace io = Alina::IO;
167
168 po::options_description desc("Options");
169
170 desc.add_options()("help,h", "show help")(
171 "binary,B",
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. ")(
175 "scale,s",
176 po::bool_switch()->default_value(false),
177 "Scale the matrix so that the diagonal is unit. ")(
178 "matrix,A",
179 po::value<string>()->required(),
180 "The system matrix in MatrixMarket format")(
181 "rhs,f",
182 po::value<string>(),
183 "The right-hand side in MatrixMarket format")(
184 "pmask,m",
185 po::value<string>(),
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.")(
188 "ub",
189 po::value<int>()->default_value(1),
190 "Block-size of the 'flow'/'non-pressure' part of the matrix")(
191 "pb",
192 po::value<int>()->default_value(1),
193 "Block-size of the 'pressure' part of the matrix")(
194 "params,P",
195 po::value<string>(),
196 "parameter file in json format")(
197 "prm,p",
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");
203
204 po::variables_map vm;
205 po::store(po::parse_command_line(argc, argv, desc), vm);
206
207 if (vm.count("help")) {
208 std::cout << desc << std::endl;
209 return 0;
210 }
211
212 po::notify(vm);
213
215 if (vm.count("params"))
216 prm.read_json(vm["params"].as<string>());
217
218 if (vm.count("prm")) {
219 for (const string& v : vm["prm"].as<vector<string>>()) {
220 prm.putKeyValue(v);
221 }
222 }
223
224 size_t rows;
225 vector<ptrdiff_t> ptr, col;
226 vector<double> val, rhs;
227 std::vector<char> pm;
228
229 {
230 auto t = prof.scoped_tic("reading");
231
232 string Afile = vm["matrix"].as<string>();
233 bool binary = vm["binary"].as<bool>();
234
235 if (binary) {
236 io::read_crs(Afile, rows, ptr, col, val);
237 }
238 else {
239 size_t cols;
240 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
241 precondition(rows == cols, "Non-square system matrix");
242 }
243
244 if (vm.count("rhs")) {
245 string bfile = vm["rhs"].as<string>();
246
247 size_t n, m;
248
249 if (binary) {
250 io::read_dense(bfile, n, m, rhs);
251 }
252 else {
253 std::tie(n, m) = io::mm_reader(bfile)(rhs);
254 }
255
256 precondition(n == rows && m == 1, "The RHS vector has wrong size");
257 }
258 else {
259 rhs.resize(rows, 1.0);
260 }
261
262 if (vm.count("pmask")) {
263 std::string pmask = vm["pmask"].as<string>();
264 prm.put("precond.pmask_size", rows);
265
266 switch (pmask[0]) {
267 case '%':
268 case '<':
269 case '>':
270 prm.put("precond.pmask_pattern", pmask);
271 break;
272 default: {
273 size_t n, m;
274 std::tie(n, m) = Alina::IO::mm_reader(pmask)(pm);
275 precondition(n == rows && m == 1, "Mask file has wrong size");
276 prm.put("precond.pmask", static_cast<void*>(&pm[0]));
277 }
278 }
279 }
280 }
281
282 if (vm["scale"].as<bool>()) {
283 std::vector<double> dia(rows, 1.0);
284
285 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
286 double d = 1.0;
287 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
288 if (col[j] == i) {
289 d = 1 / sqrt(val[j]);
290 }
291 }
292 if (!std::isnan(d))
293 dia[i] = d;
294 }
295
296 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
297 rhs[i] *= dia[i];
298 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
299 val[j] *= dia[i] * dia[col[j]];
300 }
301 }
302 }
303
304 solve_schur(vm["ub"].as<int>(), vm["pb"].as<int>(),
305 std::tie(rows, ptr, col, val), rhs, prm);
306
307 std::cout << prof << std::endl;
308}
Matrix market reader.
Definition IO.h:52
Convenience class that bundles together a preconditioner and an iterative solver.
Runtime-configurable preconditioners.
Result of a solving.
Definition AlinaUtils.h:52
Matrix class, to be used by user.
__host__ __device__ double sqrt(double v)
Racine carrée de v.
Definition Math.h:135
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Definition CSRMatrix.h:98
Runtime-configurable wrappers around iterative solvers.