Arcane  4.1.11.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: {
118 solve_schur<USolver, PSolver>(K, rhs, prm);
119 } break;
120#if defined(SOLVER_BACKEND_BUILTIN)
121 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_PSOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
122#endif
123 default:
124 precondition(false, "Unsupported block size for pressure");
125 }
126}
127
128#define ARCCORE_ALINA_BLOCK_USOLVER(z, data, B) \
129 case B: { \
130 typedef Backend<BlockMatrix<float, B, B>> BBackend; \
131 typedef ::Arcane::Alina::make_block_solver< \
132 ::Arcane::Alina::PreconditionerRuntime<BBackend>, \
133 ::Arcane::Alina::SolverRuntime<BBackend>> \
134 USolver; \
135 solve_schur<USolver>(pb, K, rhs, prm); \
136 } break;
137
138//---------------------------------------------------------------------------
139template <class Matrix>
140void solve_schur(int ub, int pb, const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
141{
142 switch (ub) {
143 case 1: {
146 solve_schur<USolver>(pb, K, rhs, prm);
147 } break;
148#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
149 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_BLOCK_USOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
150#endif
151 default:
152 precondition(false, "Unsupported block size for flow");
153 }
154}
155
156//---------------------------------------------------------------------------
157int main(int argc, char* argv[])
158{
159 auto& prof = Alina::Profiler::globalProfiler();
160 using std::string;
161 using std::vector;
162
163 namespace po = boost::program_options;
164 namespace io = Alina::IO;
165
166 po::options_description desc("Options");
167
168 desc.add_options()("help,h", "show help")(
169 "binary,B",
170 po::bool_switch()->default_value(false),
171 "When specified, treat input files as binary instead of as MatrixMarket. "
172 "It is assumed the files were converted to binary format with mm2bin utility. ")(
173 "scale,s",
174 po::bool_switch()->default_value(false),
175 "Scale the matrix so that the diagonal is unit. ")(
176 "matrix,A",
177 po::value<string>()->required(),
178 "The system matrix in MatrixMarket format")(
179 "rhs,f",
180 po::value<string>(),
181 "The right-hand side in MatrixMarket format")(
182 "pmask,m",
183 po::value<string>(),
184 "The pressure mask in MatrixMarket format. Or, if the parameter has "
185 "the form '%n:m', then each (n+i*m)-th variable is treated as pressure.")(
186 "ub",
187 po::value<int>()->default_value(1),
188 "Block-size of the 'flow'/'non-pressure' part of the matrix")(
189 "pb",
190 po::value<int>()->default_value(1),
191 "Block-size of the 'pressure' part of the matrix")(
192 "params,P",
193 po::value<string>(),
194 "parameter file in json format")(
195 "prm,p",
196 po::value<vector<string>>()->multitoken(),
197 "Parameters specified as name=value pairs. "
198 "May be provided multiple times. Examples:\n"
199 " -p solver.tol=1e-3\n"
200 " -p precond.coarse_enough=300");
201
202 po::variables_map vm;
203 po::store(po::parse_command_line(argc, argv, desc), vm);
204
205 if (vm.count("help")) {
206 std::cout << desc << std::endl;
207 return 0;
208 }
209
210 po::notify(vm);
211
213 if (vm.count("params"))
214 prm.read_json(vm["params"].as<string>());
215
216 if (vm.count("prm")) {
217 for (const string& v : vm["prm"].as<vector<string>>()) {
218 prm.putKeyValue(v);
219 }
220 }
221
222 size_t rows;
223 vector<ptrdiff_t> ptr, col;
224 vector<double> val, rhs;
225 std::vector<char> pm;
226
227 {
228 auto t = prof.scoped_tic("reading");
229
230 string Afile = vm["matrix"].as<string>();
231 bool binary = vm["binary"].as<bool>();
232
233 if (binary) {
234 io::read_crs(Afile, rows, ptr, col, val);
235 }
236 else {
237 size_t cols;
238 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
239 precondition(rows == cols, "Non-square system matrix");
240 }
241
242 if (vm.count("rhs")) {
243 string bfile = vm["rhs"].as<string>();
244
245 size_t n, m;
246
247 if (binary) {
248 io::read_dense(bfile, n, m, rhs);
249 }
250 else {
251 std::tie(n, m) = io::mm_reader(bfile)(rhs);
252 }
253
254 precondition(n == rows && m == 1, "The RHS vector has wrong size");
255 }
256 else {
257 rhs.resize(rows, 1.0);
258 }
259
260 if (vm.count("pmask")) {
261 std::string pmask = vm["pmask"].as<string>();
262 prm.put("precond.pmask_size", rows);
263
264 switch (pmask[0]) {
265 case '%':
266 case '<':
267 case '>':
268 prm.put("precond.pmask_pattern", pmask);
269 break;
270 default: {
271 size_t n, m;
272 std::tie(n, m) = Alina::IO::mm_reader(pmask)(pm);
273 precondition(n == rows && m == 1, "Mask file has wrong size");
274 prm.put("precond.pmask", static_cast<void*>(&pm[0]));
275 }
276 }
277 }
278 }
279
280 if (vm["scale"].as<bool>()) {
281 std::vector<double> dia(rows, 1.0);
282
283 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
284 double d = 1.0;
285 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
286 if (col[j] == i) {
287 d = 1 / sqrt(val[j]);
288 }
289 }
290 if (!std::isnan(d))
291 dia[i] = d;
292 }
293
294 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
295 rhs[i] *= dia[i];
296 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
297 val[j] *= dia[i] * dia[col[j]];
298 }
299 }
300 }
301
302 solve_schur(vm["ub"].as<int>(), vm["pb"].as<int>(),
303 std::tie(rows, ptr, col, val), rhs, prm);
304
305 std::cout << prof << std::endl;
306}
Matrix market reader.
Definition IO.h:54
Convenience class that bundles together a preconditioner and an iterative solver.
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.