24#pragma GCC diagnostic ignored "-Wdeprecated-copy"
25#pragma GCC diagnostic ignored "-Wint-in-bool-context"
27#include <boost/range/iterator_range.hpp>
28#include <boost/preprocessor/seq/for_each.hpp>
30#if defined(SOLVER_BACKEND_CUDA)
31#include "arccore/alina/CudaBackend.h"
32#include "arccore/alina/relaxation_cusparse_ilu0.h"
34#elif defined(SOLVER_BACKEND_EIGEN)
35#include "arccore/alina/EigenBackend.h"
38#ifndef SOLVER_BACKEND_BUILTIN
39#define SOLVER_BACKEND_BUILTIN
41#include "arccore/alina/BuiltinBackend.h"
42#include "arccore/alina/StaticMatrix.h"
43#include "arccore/alina/Adapters.h"
49#include <arccore/base/PlatformUtils.h>
50#include <arccore/base/String.h>
51#include <arccore/base/Convert.h>
53#include "arccore/alina/RelaxationRuntime.h"
54#include "arccore/alina/CoarseningRuntime.h"
55#include "arccore/alina/SolverRuntime.h"
56#include "arccore/alina/PreconditionerRuntime.h"
57#include "arccore/alina/PreconditionedSolver.h"
58#include "arccore/alina/AMG.h"
59#include "arccore/alina/Adapters.h"
60#include "arccore/alina/IO.h"
61#include "arccore/alina/Profiler.h"
63#include "arccore/common/internal/ProgramOptions.h"
65#include "SampleProblemCommon.h"
67#ifndef ARCCORE_ALINA_BLOCK_SIZES
68#define ARCCORE_ALINA_BLOCK_SIZES (3)(4)
73using Alina::precondition;
75#ifdef SOLVER_BACKEND_BUILTIN
77#include "./HypreComparer.h"
83 std::vector<ptrdiff_t>
const& ptr,
84 std::vector<ptrdiff_t>
const& col,
85 std::vector<double>
const& val,
86 std::vector<double>
const& rhs,
87 std::vector<double>& x,
90 auto& prof = Alina::Profiler::globalProfiler();
98 auto As = std::tie(rows, ptr, col, val);
99 auto Ab = Alina::adapter::block_matrix<value_type>(As);
101 std::tuple<size_t, double> info;
109 Solver solve(perm(Ab), prm);
112 std::cout << solve << std::endl;
114 rhs_type
const* fptr =
reinterpret_cast<rhs_type const*
>(&rhs[0]);
115 rhs_type* xptr =
reinterpret_cast<rhs_type*
>(&x[0]);
124 perm.inverse(X, xptr);
128 Solver solve(Ab, prm);
131 std::cout << solve << std::endl;
133 rhs_type
const* fptr =
reinterpret_cast<rhs_type const*
>(&rhs[0]);
134 rhs_type* xptr =
reinterpret_cast<rhs_type*
>(&x[0]);
143 std::copy(X.data(), X.data() + X.size(), xptr);
154 std::vector<ptrdiff_t>
const& ptr,
155 std::vector<ptrdiff_t>
const& col,
156 std::vector<double>
const& val,
157 std::vector<double>
const& rhs,
158 std::vector<double>& x,
161 std::cout <<
"Using scalar solve ptr_size=" <<
sizeof(ptrdiff_t)
162 <<
" ptr_type_size=" <<
sizeof(Backend::ptr_type)
163 <<
" col_type_size=" <<
sizeof(Backend::col_type)
164 <<
" value_type_size=" <<
sizeof(Backend::value_type)
166 auto& prof = Alina::Profiler::globalProfiler();
169#if defined(SOLVER_BACKEND_CUDA)
170 cusparseCreate(&bprm.cusparse_handle);
176 cudaGetDeviceProperties(&prop, dev);
177 std::cout << prop.name << std::endl
192 Solver solve(perm(std::tie(rows, ptr, col, val)), prm, bprm);
195 std::cout << solve << std::endl;
197 std::vector<double> tmp(rows);
199 perm.forward(rhs, tmp);
200 auto f_b = Backend::copy_vector(tmp, bprm);
202 perm.forward(x, tmp);
203 auto x_b = Backend::copy_vector(tmp, bprm);
206 info = solve(*f_b, *x_b);
209#if defined(SOLVER_BACKEND_CUDA)
210 thrust::copy(x_b->begin(), x_b->end(), tmp.begin());
212 std::copy(&(*x_b)[0], &(*x_b)[0] + rows, &tmp[0]);
215 perm.inverse(tmp, x);
219 Solver solve(std::tie(rows, ptr, col, val), prm, bprm);
222 std::cout << solve << std::endl;
224 auto f_b = Backend::copy_vector(rhs, bprm);
225 auto x_b = Backend::copy_vector(x, bprm);
228 info = solve(*f_b, *x_b);
231#if defined(SOLVER_BACKEND_CUDA)
232 thrust::copy(x_b->begin(), x_b->end(), x.begin());
234 std::copy(&(*x_b)[0], &(*x_b)[0] + rows, &x[0]);
241#define ARCCORE_ALINA_CALL_BLOCK_SOLVER(z, data, B) \
243 return block_solve<B>(prm, rows, ptr, col, val, rhs, x, reorder);
249 std::vector<ptrdiff_t>
const& ptr,
250 std::vector<ptrdiff_t>
const& col,
251 std::vector<double>
const& val,
252 std::vector<double>
const& rhs,
253 std::vector<double>& x,
257 switch (block_size) {
259 return scalar_solve(prm, rows, ptr, col, val, rhs, x, reorder);
260#if defined(SOLVER_BACKEND_BUILTIN)
261 BOOST_PP_SEQ_FOR_EACH(ARCCORE_ALINA_CALL_BLOCK_SOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
264 precondition(
false,
"Unsupported block size");
270int main(
int argc,
char* argv[])
272 auto& prof = Alina::Profiler::globalProfiler();
273 namespace po = Arcane::ProgramOptions;
274 namespace io = Alina::IO;
281 desc.add_options()(
"help,h",
"Show this help.")(
"prm-file,P",
283 "Parameter file in json format. ")(
285 po::value<vector<string>>()->multitoken(),
286 "Parameters specified as name=value pairs. "
287 "May be provided multiple times. Examples:\n"
288 " -p solver.tol=1e-3\n"
289 " -p precond.coarse_enough=300")(
"matrix,A",
291 "System matrix in the MatrixMarket format. "
292 "When not specified, solves a Poisson problem in 3D unit cube. ")(
295 "The RHS vector in the MatrixMarket format. "
296 "When omitted, a vector of ones is used by default. "
297 "Should only be provided together with a system matrix. ")(
299 po::bool_switch()->default_value(
false),
300 "Use zero RHS vector. Implies --random-initial and solver.ns_search=true")(
302 po::bool_switch()->default_value(
false),
303 "Set RHS = Ax where x = 1")(
306 "The near null-space vectors in the MatrixMarket format. "
307 "Should be a dense matrix of size N*M, where N is the number of "
308 "unknowns, and M is the number of null-space vectors. "
309 "Should only be provided together with a system matrix. ")(
312 "Coordinate matrix where number of rows corresponds to the number of grid nodes "
313 "and the number of columns corresponds to the problem dimensionality (2 or 3). "
314 "Will be used to construct near null-space vectors as rigid body modes. "
315 "Should only be provided together with a system matrix. ")(
317 po::bool_switch()->default_value(
false),
318 "When specified, treat input files as binary instead of as MatrixMarket. "
319 "It is assumed the files were converted to binary format with mm2bin utility. ")(
321 po::bool_switch()->default_value(
false),
322 "Scale the matrix so that the diagonal is unit. ")(
324 po::value<int>()->default_value(1),
325 "The block size of the system matrix. "
326 "When specified, the system matrix is assumed to have block-wise structure. "
327 "This usually is the case for problems in elasticity, structural mechanics, "
328 "for coupled systems of PDE (such as Navier-Stokes equations), etc. ")(
330 po::value<int>()->default_value(32),
331 "The size of the Poisson problem to solve when no system matrix is given. "
332 "Specified as number of grid nodes along each dimension of a unit cube. "
333 "The resulting system will have n*n*n unknowns. ")(
335 po::value<double>()->default_value(1.0),
336 "The anisotropy value for the generated Poisson value. "
337 "Used to determine problem scaling along X, Y, and Z axes: "
338 "hy = hx * a, hz = hy * a.")(
340 po::bool_switch()->default_value(
false),
341 "When specified, the AMG hierarchy is not constructed. "
342 "Instead, the problem is solved using a single-level smoother as preconditioner. ")(
344 po::bool_switch()->default_value(
false),
345 "When specified, the matrix will be reordered to improve cache-locality")(
347 po::value<double>()->default_value(0),
348 "Value to use as initial approximation. ")(
350 po::bool_switch()->default_value(
false),
351 "Use random initial approximation. ")(
354 "Output file. Will be saved in the MatrixMarket format. "
355 "When omitted, the solution is not saved. ");
364 if (vm.count(
"help")) {
365 std::cout << desc << std::endl;
369 for (
int i = 0; i < argc; ++i) {
372 std::cout << argv[i];
374 std::cout << std::endl;
377 if (vm.count(
"prm-file")) {
378 prm.read_json(vm[
"prm-file"].as<string>());
381 if (vm.count(
"prm")) {
382 for (
const string& v : vm[
"prm"].as<vector<string>>()) {
388 vector<ptrdiff_t> ptr, col;
389 vector<double> val, rhs, null, x;
391 if (vm.count(
"matrix")) {
392 auto t = prof.scoped_tic(
"reading");
394 string Afile = vm[
"matrix"].as<
string>();
395 bool binary = vm[
"binary"].as<
bool>();
398 io::read_crs(Afile, rows, ptr, col, val);
402 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
403 precondition(rows == cols,
"Non-square system matrix");
406 if (vm.count(
"rhs")) {
407 string bfile = vm[
"rhs"].as<
string>();
412 io::read_dense(bfile, n, m, rhs);
415 std::tie(n, m) = io::mm_reader(bfile)(rhs);
418 precondition(n == rows && m == 1,
"The RHS vector has wrong size");
420 else if (vm[
"f1"].as<bool>()) {
422 for (
size_t i = 0; i < rows; ++i) {
424 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j)
430 rhs.resize(rows, vm[
"f0"].as<bool>() ? 0.0 : 1.0);
433 if (vm.count(
"null")) {
434 string nfile = vm[
"null"].as<
string>();
439 io::read_dense(nfile, m, nv, null);
442 std::tie(m, nv) = io::mm_reader(nfile)(null);
445 precondition(m == rows,
"Near null-space vectors have wrong size");
447 else if (vm.count(
"coords")) {
448 string cfile = vm[
"coords"].as<
string>();
449 std::vector<double> coo;
454 io::read_dense(cfile, m, ndim, coo);
457 std::tie(m, ndim) = io::mm_reader(cfile)(coo);
460 precondition(m * ndim == rows && (ndim == 2 || ndim == 3),
"Coordinate matrix has wrong size");
462 nv = Alina::rigid_body_modes(ndim, coo, null);
466 prm.put(
"precond.coarsening.nullspace.cols", nv);
467 prm.put(
"precond.coarsening.nullspace.rows", rows);
468 prm.put(
"precond.coarsening.nullspace.B", &null[0]);
472 auto t = prof.scoped_tic(
"assembling");
473 rows = sample_problem(vm[
"size"].as<int>(), val, col, ptr, rhs, vm[
"anisotropy"].as<double>());
476 if (vm[
"scale"].as<bool>()) {
477 std::vector<double> dia(rows, 1.0);
479 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
481 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
483 d = 1 /
sqrt(val[j]);
490 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(rows); ++i) {
492 for (ptrdiff_t j = ptr[i], e = ptr[i + 1]; j < e; ++j) {
493 val[j] *= dia[i] * dia[col[j]];
498 x.resize(rows, vm[
"initial"].as<double>());
499 if (vm[
"random-initial"].as<bool>() || vm[
"f0"].as<bool>()) {
501 std::uniform_real_distribution<double> rnd(-1, 1);
506 if (vm[
"f0"].as<bool>()) {
507 prm.put(
"solver.ns_search",
true);
510 int block_size = vm[
"block-size"].as<
int>();
511 std::cout <<
"BlockSize= " << block_size <<
"\n";
513 if (vm[
"single-level"].as<bool>())
514 prm.put(
"precond.class",
"relaxation");
517 bool do_hypre =
false;
519 do_hypre = v.value();
522#ifndef SOLVER_BACKEND_BUILTIN
526#ifdef SOLVER_BACKEND_BUILTIN
528 hypre_comparer.solve(rows, ptr, col, val, rhs, x, argc, argv);
532 solver_result = solve(prm, rows, ptr, col, val, rhs, x, block_size, vm[
"reorder"].as<bool>());
534 if (vm.count(
"output")) {
535 auto t = prof.scoped_tic(
"write");
536 Alina::IO::mm_write(vm[
"output"].as<string>(), &x[0], x.size());
539 std::cout <<
"Iterations: " << solver_result.nbIteration() << std::endl
540 <<
"Error: " << solver_result.residual() << std::endl
541 << prof << std::endl;
Convenience class that bundles together a preconditioner and an iterative solver.
Class to store parameters as a hierarchical key/value tree.
NUMA-aware vector container.
Template class for converting a type.
Fluent command-line parser builder.
Describes a set of command-line options.
Describes positional (non-option) arguments.
Stores parsed option values.
View of an array of elements of type T.
Unicode character string.
apfloat sqrt(apfloat v)
Square root of v.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Alina::detail::empty_params params
Runtime-configurable wrappers around iterative solvers.