Arcane  4.2.2.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::PropertyTree& prm,
65 size_t rows,
66 std::vector<ptrdiff_t> const& ptr,
67 std::vector<ptrdiff_t> const& col,
68 std::vector<double> const& val,
69 std::vector<double> const& rhs,
70 std::vector<double>& x)
71{
72 std::cout << "Using scalar solve ptr_size=" << sizeof(ptrdiff_t)
73 << " ptr_type_size=" << sizeof(Backend::ptr_type)
74 << " col_type_size=" << sizeof(Backend::col_type)
75 << " value_type_size=" << sizeof(Backend::value_type)
76 << "\n";
77 auto& prof = Alina::Profiler::globalProfiler();
78 Backend::params bprm;
79
81 //using Solver = Alina::PreconditionedSolver<Precond, Alina::BiCGStabSolver<Backend>>;
83 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::SolverRuntime<Backend>>;
84 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::BiCGStabSolver<Backend>>;
85
87
88 {
89 prof.tic("setup");
90 Solver solve(std::tie(rows, ptr, col, val), prm, bprm);
91 prof.toc("setup");
92
93 std::cout << "Printing solver infos\n";
94 std::cout << solve << std::endl;
96 solve.prm.get(ptree);
97 std::cout << "SOLVER PARAMS: " << ptree << "\n";
98
99 auto f_b = Backend::copy_vector(rhs, bprm);
100 auto x_b = Backend::copy_vector(x, bprm);
101
102 prof.tic("solve");
103 info = solve(*f_b, *x_b);
104 prof.toc("solve");
105 }
106
107 return info;
108}
109
110//---------------------------------------------------------------------------
111
112int main2(const Alina::SampleMainContext& ctx, int argc, char* argv[])
113{
114 ITraceMng* tm = ctx.traceMng();
115
116 auto& prof = Alina::Profiler::globalProfiler();
117 namespace po = Arcane::ProgramOptions;
118 namespace io = Alina::IO;
119
120 using std::string;
121 using std::vector;
122
123 po::options_description desc("Options");
124
125 desc.add_options()("help,h", "Show this help.");
126 desc.add_options()("prm-file,P", po::value<string>(),
127 "Parameter file in json format. ");
128 desc.add_options()("prm,p", po::value<vector<string>>()->multitoken(),
129 "Parameters specified as name=value pairs. "
130 "May be provided multiple times. Examples:\n"
131 " -p solver.tol=1e-3\n"
132 " -p precond.coarse_enough=300");
133
134 desc.add_options()("size,n",
135 po::value<int>()->default_value(32),
136 "The size of the Poisson problem to solve when no system matrix is given. "
137 "Specified as number of grid nodes along each dimension of a unit cube. "
138 "The resulting system will have n*n*n unknowns. ");
139
140 desc.add_options()("anisotropy,a",
141 po::value<double>()->default_value(1.0),
142 "The anisotropy value for the generated Poisson value. "
143 "Used to determine problem scaling along X, Y, and Z axes: "
144 "hy = hx * a, hz = hy * a.");
145
147 p.add("prm", -1);
148
150 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
151 po::notify(vm);
152
153 if (vm.count("help")) {
154 tm->info() << desc;
155 return 0;
156 }
157
158 for (int i = 0; i < argc; ++i) {
159 if (i)
160 std::cout << " ";
161 std::cout << argv[i];
162 }
163 std::cout << std::endl;
164
166 if (vm.count("prm-file")) {
167 prm.read_json(vm["prm-file"].as<string>());
168 }
169
170 if (vm.count("prm")) {
171 for (const string& v : vm["prm"].as<vector<string>>()) {
172 prm.putKeyValue(v);
173 }
174 }
175
176 size_t rows = vm["size"].as<int>();
177 vector<ptrdiff_t> ptr, col;
178 vector<double> val, rhs, null, x;
179 std::cout << "ROWS=" << rows << "\n";
180 {
181 auto t = prof.scoped_tic("assembling");
182 rows = sample_problem(rows, val, col, ptr, rhs, vm["anisotropy"].as<double>());
183 }
184
185 x.resize(rows, 0.0);
186
187 String do_hypre_str = Platform::getEnvironmentVariable("ALINA_USE_HYPRE");
188 bool do_hypre = false;
189 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ALINA_USE_HYPRE", true))
190 do_hypre = v.value();
191
192 Alina::SolverResult solver_result;
193 if (do_hypre) {
194 HypreComparer hypre_comparer(false);
195 hypre_comparer.solve(rows, ptr, col, val, rhs, x, argc, argv);
196 }
197 else {
198
199 solver_result = solve(prm, rows, ptr, col, val, rhs, x);
200
201 if (vm.count("output")) {
202 auto t = prof.scoped_tic("write");
203 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
204 }
205 }
206 std::cout << "Iterations: " << solver_result.nbIteration() << std::endl
207 << "Error: " << solver_result.residual() << std::endl
208 << prof << std::endl;
209 return 0;
210}
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
214
215int main(int argc, char* argv[])
216{
217 return Arcane::Alina::SampleMainContext::execMain(main2, argc, argv);
218}
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
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:112
Result of a solution.
Definition AlinaUtils.h:53
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 --