Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
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 "arccore/base/PlatformUtils.h"
23#include "arccore/base/String.h"
24#include "arccore/base/Convert.h"
25
26#include "arccore/alina/BuiltinBackend.h"
27#include "arccore/alina/StaticMatrix.h"
28#include "arccore/alina/Adapters.h"
29#include "arccore/alina/Relaxation.h"
30#include "arccore/alina/Coarsening.h"
31#include "arccore/alina/BiCGStabSolver.h"
32#include "arccore/alina/PreconditionedSolver.h"
33#include "arccore/alina/AMG.h"
34#include "arccore/alina/Adapters.h"
35#include "arccore/alina/IO.h"
36
37#include "arccore/alina/SolverRuntime.h"
38#include "arccore/alina/PreconditionerRuntime.h"
39
40#include "arccore/alina/Profiler.h"
41
42#include "arccore/common/internal/ProgramOptions.h"
43
44#include "arccore/trace/ITraceMng.h"
45
46#include "./AlinaSamplesCommon.h"
47#include "./HypreComparer.h"
48
49#include "SampleProblemCommon.h"
50
51using namespace Arcane;
52using Alina::precondition;
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
64solve(const Alina::AlinaContext& context,
65 const Alina::PropertyTree& prm,
66 size_t rows,
67 std::vector<ptrdiff_t> const& ptr,
68 std::vector<ptrdiff_t> const& col,
69 std::vector<double> const& val,
70 std::vector<double> const& rhs,
71 std::vector<double>& x)
72{
73 std::cout << "Using scalar solve ptr_size=" << sizeof(ptrdiff_t)
74 << " ptr_type_size=" << sizeof(Backend::ptr_type)
75 << " col_type_size=" << sizeof(Backend::col_type)
76 << " value_type_size=" << sizeof(Backend::value_type)
77 << "\n";
78 auto& prof = Alina::Profiler::globalProfiler();
79 Backend::params bprm;
80
82 //using Solver = Alina::PreconditionedSolver<Precond, Alina::BiCGStabSolver<Backend>>;
84 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::SolverRuntime<Backend>>;
85 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::BiCGStabSolver<Backend>>;
86
88
89 {
90 prof.tic("setup");
91 Solver solve(context, std::tie(rows, ptr, col, val), prm, bprm);
92 prof.toc("setup");
93
94 std::cout << "Printing solver infos\n";
95 std::cout << solve << std::endl;
97 solve.prm.get(ptree);
98 std::cout << "SOLVER PARAMS: " << ptree << "\n";
99
100 auto f_b = Backend::copy_vector(rhs, bprm);
101 auto x_b = Backend::copy_vector(x, bprm);
102
103 prof.tic("solve");
104 info = solve(*f_b, *x_b);
105 prof.toc("solve");
106 }
107
108 return info;
109}
110
111//---------------------------------------------------------------------------
112
113int main2(const Alina::SampleMainContext& ctx, int argc, char* argv[])
114{
115 ITraceMng* tm = ctx.traceMng();
116
117 auto& prof = Alina::Profiler::globalProfiler();
118 namespace po = Arcane::ProgramOptions;
119
120 Alina::AlinaContext context;
121
122 using std::string;
123 using std::vector;
124
125 po::options_description desc("Options");
126
127 desc.add_options()("help,h", "Show this help.");
128 desc.add_options()("prm-file,P", po::value<string>(),
129 "Parameter file in json format. ");
130 desc.add_options()("prm,p", po::value<vector<string>>()->multitoken(),
131 "Parameters specified as name=value pairs. "
132 "May be provided multiple times. Examples:\n"
133 " -p solver.tol=1e-3\n"
134 " -p precond.coarse_enough=300");
135
136 desc.add_options()("size,n",
137 po::value<int>()->default_value(32),
138 "The size of the Poisson problem to solve when no system matrix is given. "
139 "Specified as number of grid nodes along each dimension of a unit cube. "
140 "The resulting system will have n*n*n unknowns. ");
141
142 desc.add_options()("anisotropy,a",
143 po::value<double>()->default_value(1.0),
144 "The anisotropy value for the generated Poisson value. "
145 "Used to determine problem scaling along X, Y, and Z axes: "
146 "hy = hx * a, hz = hy * a.");
147
149 p.add("prm", -1);
150
152 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
153 po::notify(vm);
154
155 if (vm.count("help")) {
156 tm->info() << desc;
157 return 0;
158 }
159
160 for (int i = 0; i < argc; ++i) {
161 if (i)
162 std::cout << " ";
163 std::cout << argv[i];
164 }
165 std::cout << std::endl;
166
168 if (vm.count("prm-file")) {
169 prm.read_json(vm["prm-file"].as<string>());
170 }
171
172 if (vm.count("prm")) {
173 for (const string& v : vm["prm"].as<vector<string>>()) {
174 prm.putKeyValue(v);
175 }
176 }
177
178 size_t rows = vm["size"].as<int>();
179 vector<ptrdiff_t> ptr, col;
180 vector<double> val, rhs, null, x;
181 std::cout << "ROWS=" << rows << "\n";
182 {
183 auto t = prof.scoped_tic("assembling");
184 rows = Alina::PoissonProblemGenerator::createSequentialMatrix(rows, val, col, ptr, rhs, vm["anisotropy"].as<double>());
185 }
186
187 x.resize(rows, 0.0);
188
189 String do_hypre_str = Platform::getEnvironmentVariable("ALINA_USE_HYPRE");
190 bool do_hypre = false;
191 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ALINA_USE_HYPRE", true))
192 do_hypre = v.value();
193
194 Alina::SolverResult solver_result;
195 if (do_hypre) {
196 HypreComparer hypre_comparer(false);
197 hypre_comparer.solve(rows, ptr, col, val, rhs, x, argc, argv);
198 }
199 else {
200
201 solver_result = solve(context, prm, rows, ptr, col, val, rhs, x);
202
203 if (vm.count("output")) {
204 auto t = prof.scoped_tic("write");
205 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
206 }
207 }
208 std::cout << "Iterations: " << solver_result.nbIteration() << std::endl
209 << "Error: " << solver_result.residual() << std::endl
210 << prof << std::endl;
211 return 0;
212}
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217int main(int argc, char* argv[])
218{
219 return Arcane::Alina::SampleMainContext::execMain(main2, argc, argv);
220}
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
Class to store parameters as a hierarchical key/value tree.
Definition AlinaUtils.h:113
Result of a solution.
Definition AlinaUtils.h:56
Template class for converting a type.
virtual TraceMessage info()=0
Stream for an information message.
Fluent command-line parser builder.
Describes a set of command-line options.
Describes positional (non-option) arguments.
Stores parsed option values.
String getEnvironmentVariable(const String &name)
Environment variable named name.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --