Arcane  4.1.11.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicSolver.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
24#include "arccore/alina/BuiltinBackend.h"
25#include "arccore/alina/StaticMatrix.h"
26#include "arccore/alina/Adapters.h"
27
28#include "arcane/utils/PlatformUtils.h"
29#include "arcane/utils/String.h"
30#include "arcane/utils/Convert.h"
31
32#include "arccore/alina/Relaxation.h"
33#include "arccore/alina/Coarsening.h"
34#include "arccore/alina/BiCGStabSolver.h"
35#include "arccore/alina/PreconditionedSolver.h"
36#include "arccore/alina/AMG.h"
37#include "arccore/alina/Adapters.h"
38#include "arccore/alina/IO.h"
39
40#include "arccore/alina/SolverRuntime.h"
41#include "arccore/alina/PreconditionerRuntime.h"
42
43#include "arccore/alina/Profiler.h"
44
45#include "AlinaSamplesCommon.h"
46#include "arccore/trace/ITraceMng.h"
47
48#include "SampleProblemCommon.h"
49
50using namespace Arcane;
51using Alina::precondition;
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62extern "C++" void
63_doHypreSolver(int nb_row,
64 std::vector<ptrdiff_t> const& ptr,
65 std::vector<ptrdiff_t> const& col,
66 std::vector<double> const& val,
67 std::vector<double> const& rhs,
68 std::vector<double>& x,
69 int argc, char* argv[]);
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
75solve(const Alina::PropertyTree& prm,
76 size_t rows,
77 std::vector<ptrdiff_t> const& ptr,
78 std::vector<ptrdiff_t> const& col,
79 std::vector<double> const& val,
80 std::vector<double> const& rhs,
81 std::vector<double>& x)
82{
83 std::cout << "Using scalar solve ptr_size=" << sizeof(ptrdiff_t)
84 << " ptr_type_size=" << sizeof(Backend::ptr_type)
85 << " col_type_size=" << sizeof(Backend::col_type)
86 << " value_type_size=" << sizeof(Backend::value_type)
87 << "\n";
88 auto& prof = Alina::Profiler::globalProfiler();
89 Backend::params bprm;
90
92 //using Solver = Alina::PreconditionedSolver<Precond, Alina::BiCGStabSolver<Backend>>;
94 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::SolverRuntime<Backend>>;
95 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::BiCGStabSolver<Backend>>;
96
98
99 {
100 prof.tic("setup");
101 Solver solve(std::tie(rows, ptr, col, val), prm, bprm);
102 prof.toc("setup");
103
104 std::cout << "Printing solver infos\n";
105 std::cout << solve << std::endl;
107 solve.prm.get(ptree);
108 std::cout << "SOLVER PARAMS: " << ptree << "\n";
109
110 auto f_b = Backend::copy_vector(rhs, bprm);
111 auto x_b = Backend::copy_vector(x, bprm);
112
113 prof.tic("solve");
114 info = solve(*f_b, *x_b);
115 prof.toc("solve");
116 }
117
118 return info;
119}
120
121//---------------------------------------------------------------------------
122
123int main2(const Alina::SampleMainContext& ctx, int argc, char* argv[])
124{
125 ITraceMng* tm = ctx.traceMng();
126
127 auto& prof = Alina::Profiler::globalProfiler();
128 namespace po = boost::program_options;
129 namespace io = Alina::IO;
130
131 using std::string;
132 using std::vector;
133
134 po::options_description desc("Options");
135
136 desc.add_options()("help,h", "Show this help.");
137 desc.add_options()("prm-file,P", po::value<string>(),
138 "Parameter file in json format. ");
139 desc.add_options()("prm,p", po::value<vector<string>>()->multitoken(),
140 "Parameters specified as name=value pairs. "
141 "May be provided multiple times. Examples:\n"
142 " -p solver.tol=1e-3\n"
143 " -p precond.coarse_enough=300");
144
145 desc.add_options()("size,n",
146 po::value<int>()->default_value(32),
147 "The size of the Poisson problem to solve when no system matrix is given. "
148 "Specified as number of grid nodes along each dimension of a unit cube. "
149 "The resulting system will have n*n*n unknowns. ");
150
151 desc.add_options()("anisotropy,a",
152 po::value<double>()->default_value(1.0),
153 "The anisotropy value for the generated Poisson value. "
154 "Used to determine problem scaling along X, Y, and Z axes: "
155 "hy = hx * a, hz = hy * a.");
156
157 po::positional_options_description p;
158 p.add("prm", -1);
159
160 po::variables_map vm;
161 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
162 po::notify(vm);
163
164 if (vm.count("help")) {
165 tm->info() << desc;
166 return 0;
167 }
168
169 for (int i = 0; i < argc; ++i) {
170 if (i)
171 std::cout << " ";
172 std::cout << argv[i];
173 }
174 std::cout << std::endl;
175
177 if (vm.count("prm-file")) {
178 prm.read_json(vm["prm-file"].as<string>());
179 }
180
181 if (vm.count("prm")) {
182 for (const string& v : vm["prm"].as<vector<string>>()) {
183 prm.putKeyValue(v);
184 }
185 }
186
187 size_t rows = vm["size"].as<int>();
188 vector<ptrdiff_t> ptr, col;
189 vector<double> val, rhs, null, x;
190 std::cout << "ROWS=" << rows << "\n";
191 {
192 auto t = prof.scoped_tic("assembling");
193 rows = sample_problem(rows, val, col, ptr, rhs, vm["anisotropy"].as<double>());
194 }
195
196 x.resize(rows, 0.0);
197
198 String do_hypre_str = Platform::getEnvironmentVariable("ALINA_USE_HYPRE");
199 bool do_hypre = false;
200 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ALINA_USE_HYPRE", true))
201 do_hypre = v.value();
202
203 Alina::SolverResult solver_result;
204 if (do_hypre) {
205 _doHypreSolver(rows, ptr, col, val, rhs, x, argc, argv);
206 }
207 else {
208
209 solver_result = solve(prm, rows, ptr, col, val, rhs, x);
210
211 if (vm.count("output")) {
212 auto t = prof.scoped_tic("write");
213 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
214 }
215 }
216 std::cout << "Iterations: " << solver_result.nbIteration() << std::endl
217 << "Error: " << solver_result.residual() << std::endl
218 << prof << std::endl;
219 return 0;
220}
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224
225int main(int argc, char* argv[])
226{
227 return Arcane::Alina::SampleMainContext::execMain(main2, argc, argv);
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
Result of a solving.
Definition AlinaUtils.h:52
Classe template pour convertir un type.
Interface du gestionnaire de traces.
virtual TraceMessage info()=0
Flot pour un message d'information.
Chaîne de caractères unicode.
String getEnvironmentVariable(const String &name)
Variable d'environnement du nom name.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Alina::detail::empty_params params