Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
CPR.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#include <iostream>
20#include <string>
21
22#include <boost/program_options.hpp>
23#include <boost/preprocessor/seq/for_each.hpp>
24
25#include "arccore/alina/BuiltinBackend.h"
26#include "arccore/alina/StaticMatrix.h"
27#include "arccore/alina/PreconditionedSolver.h"
28#include "arccore/alina/AMG.h"
29#include "arccore/alina/SolverRuntime.h"
30#include "arccore/alina/CoarseningRuntime.h"
31#include "arccore/alina/RelaxationRuntime.h"
32#include "arccore/alina/Relaxation.h"
33#include "arccore/alina/CPRPreconditioner.h"
34#include "arccore/alina/Adapters.h"
35#include "arccore/alina/IO.h"
36#include "arccore/alina/Profiler.h"
37
38using namespace Arcane;
39
40using Alina::precondition;
41
42//---------------------------------------------------------------------------
43template <class Matrix>
44void solve_cpr(const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
45{
46 auto& prof = Alina::Profiler::globalProfiler();
47
48 auto t1 = prof.scoped_tic("CPR");
49
50 using Backend = Alina::BuiltinBackend<double>;
53
54 prof.tic("setup");
57 solve(K, prm);
58 prof.toc("setup");
59
60 std::cout << solve.precond() << std::endl;
61
62 std::vector<double> x(rhs.size(), 0.0);
63
64 prof.tic("setup");
65 Alina::SolverResult r = solve(rhs, x);
66 prof.toc("setup");
67
68 std::cout << "Iterations: " << r.nbIteration() << std::endl
69 << "Error: " << r.residual() << std::endl;
70}
71
72//---------------------------------------------------------------------------
73template <int B, class Matrix>
74void solve_block_cpr(const Matrix& K, const std::vector<double>& rhs, Alina::PropertyTree& prm)
75{
76 auto& prof = Alina::Profiler::globalProfiler();
77 auto t1 = prof.scoped_tic("CPR");
78
79 typedef Alina::StaticMatrix<double, B, B> val_type;
80 typedef Alina::StaticMatrix<double, B, 1> rhs_type;
81 typedef Alina::BuiltinBackend<val_type> SBackend;
82 typedef Alina::BuiltinBackend<double> PBackend;
83
86
87 prof.tic("setup");
91 solve(Alina::adapter::block_matrix<val_type>(K), prm);
92 prof.toc("setup");
93
94 std::cout << solve.precond() << std::endl;
95
96 std::vector<rhs_type> x(rhs.size(), Alina::math::zero<rhs_type>());
97
98 auto rhs_ptr = reinterpret_cast<const rhs_type*>(rhs.data());
99 size_t n = Alina::backend::nbRow(K) / B;
100
101 prof.tic("solve");
102 Alina::SolverResult r = solve(SmallSpan<const rhs_type>(rhs_ptr, n), x);
103 prof.toc("solve");
104
105 std::cout << "Iterations: " << r.nbIteration() << std::endl
106 << "Error: " << r.residual() << std::endl;
107}
108
109//---------------------------------------------------------------------------
110int main(int argc, char* argv[])
111{
112 auto& prof = Alina::Profiler::globalProfiler();
113 using Alina::precondition;
114 using std::string;
115 using std::vector;
116
117 namespace po = boost::program_options;
118 namespace io = Alina::IO;
119
120 po::options_description desc("Options");
121
122 desc.add_options()("help,h", "show help")(
123 "binary,B",
124 po::bool_switch()->default_value(false),
125 "When specified, treat input files as binary instead of as MatrixMarket. "
126 "It is assumed the files were converted to binary format with mm2bin utility. ")(
127 "matrix,A",
128 po::value<string>()->required(),
129 "The system matrix in MatrixMarket format")(
130 "rhs,f",
131 po::value<string>(),
132 "The right-hand side in MatrixMarket format")(
133 "runtime-block-size,b",
134 po::value<int>(),
135 "The block size of the system matrix set at runtime")(
136 "static-block-size,c",
137 po::value<int>()->default_value(1),
138 "The block size of the system matrix set at compiletime")(
139 "params,P",
140 po::value<string>(),
141 "parameter file in json format")(
142 "prm,p",
143 po::value<vector<string>>()->multitoken(),
144 "Parameters specified as name=value pairs. "
145 "May be provided multiple times. Examples:\n"
146 " -p solver.tol=1e-3\n"
147 " -p precond.coarse_enough=300");
148
149 po::variables_map vm;
150 po::store(po::parse_command_line(argc, argv, desc), vm);
151
152 if (vm.count("help")) {
153 std::cout << desc << std::endl;
154 return 0;
155 }
156
157 po::notify(vm);
158
160 if (vm.count("params"))
161 prm.read_json(vm["params"].as<string>());
162
163 if (vm.count("prm")) {
164 for (const string& v : vm["prm"].as<vector<string>>()) {
165 prm.putKeyValue(v);
166 }
167 }
168
169 int cb = vm["static-block-size"].as<int>();
170
171 if (vm.count("runtime-block-size"))
172 prm.put("precond.block_size", vm["runtime-block-size"].as<int>());
173 else
174 prm.put("precond.block_size", cb);
175
176 size_t rows;
177 vector<ptrdiff_t> ptr, col;
178 vector<double> val, rhs;
179 std::vector<char> pm;
180
181 {
182 auto t = prof.scoped_tic("reading");
183
184 string Afile = vm["matrix"].as<string>();
185 bool binary = vm["binary"].as<bool>();
186
187 if (binary) {
188 io::read_crs(Afile, rows, ptr, col, val);
189 }
190 else {
191 size_t cols;
192 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
193 precondition(rows == cols, "Non-square system matrix");
194 }
195
196 if (vm.count("rhs")) {
197 string bfile = vm["rhs"].as<string>();
198
199 size_t n, m;
200
201 if (binary) {
202 io::read_dense(bfile, n, m, rhs);
203 }
204 else {
205 std::tie(n, m) = io::mm_reader(bfile)(rhs);
206 }
207
208 precondition(n == rows && m == 1, "The RHS vector has wrong size");
209 }
210 else {
211 rhs.resize(rows, 1.0);
212 }
213 }
214
215#define CALL_BLOCK_SOLVER(z, data, B) \
216 case B: \
217 solve_block_cpr<B>(std::tie(rows, ptr, col, val), rhs, prm); \
218 break;
219
220 switch (cb) {
221 case 1:
222 solve_cpr(std::tie(rows, ptr, col, val), rhs, prm);
223 break;
224
225 BOOST_PP_SEQ_FOR_EACH(CALL_BLOCK_SOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
226
227 default:
228 precondition(false, "Unsupported block size");
229 break;
230 }
231
232 std::cout << prof << std::endl;
233}
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
Allows to use an AMG smoother as standalone preconditioner.
Definition Relaxation.h:144
Result of a solving.
Definition AlinaUtils.h:52
Two stage preconditioner of the Constrained Pressure Residual type.
Matrix class, to be used by user.
Vue d'un tableau d'éléments de type T.
Definition Span.h:801
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Runtime-configurable wrappers around iterative solvers.