25#include <boost/range/iterator_range.hpp>
27#include "arccore/alina/BuiltinBackend.h"
28#include "arccore/alina/PreconditionerRuntime.h"
29#include "arccore/alina/Adapters.h"
30#include "arccore/alina/DistributedPreconditionedSolver.h"
31#include "arccore/alina/DistributedSolverRuntime.h"
32#include "arccore/alina/DistributedPreconditioner.h"
33#include "arccore/alina/Profiler.h"
34#include "arccore/alina/AlinaUtils.h"
37#include "arccore/alina/DistributedSolver.h"
40#include "arccore/alina/DistributedRelaxationRuntime.h"
42#include "arccore/common/internal/ProgramOptions.h"
44#include "DomainPartition.h"
47using namespace Arcane::Alina;
49using Alina::precondition;
54 const DomainPartition<2>& part;
55 const std::vector<ptrdiff_t>& dom;
57 renumbering(
const DomainPartition<2>& p,
58 const std::vector<ptrdiff_t>& d)
63 ptrdiff_t operator()(ptrdiff_t i, ptrdiff_t j)
const
65 boost::array<ptrdiff_t, 2> p = { { i, j } };
66 std::pair<int, ptrdiff_t> v = part.index(p);
67 return dom[v.first] + v.second;
73template <
template <
class>
class Precond,
class Matrix>
80 auto& prof = Alina::Profiler::globalProfiler();
86 const size_t n = Alina::backend::nbRow(A);
88 std::vector<double> rhs(n, 1), x(n, 0);
91 Solver solve(context, comm, A, prm);
95 auto t2 = prof.scoped_tic(
"solve");
102int main(
int argc,
char* argv[])
104 auto& prof = Alina::Profiler::globalProfiler();
105 namespace po = Arcane::ProgramOptions;
113 desc.add_options()(
"help,h",
"Show this help.");
114 desc.add_options()(
"prm-file,P",
116 "Parameter file in json format. ");
117 desc.add_options()(
"prm,p",
118 po::value<vector<string>>()->multitoken(),
119 "Parameters specified as name=value pairs. "
120 "May be provided multiple times. Examples:\n"
121 " -p solver.tol=1e-3\n"
122 " -p precond.coarse_enough=300");
123 desc.add_options()(
"size,n",
124 po::value<int>()->default_value(1024),
125 "The size of the Poisson problem to solve. "
126 "Specified as number of grid nodes along each dimension of a unit square. "
127 "The resulting system will have n*n unknowns. ");
128 desc.add_options()(
"single-level,1",
129 po::bool_switch()->default_value(
false),
130 "When specified, the AMG hierarchy is not constructed. "
131 "Instead, the problem is solved using a single-level smoother as preconditioner. ");
132 desc.add_options()(
"initial,x",
133 po::value<double>()->default_value(0),
134 "Value to use as initial approximation. ");
137 po::store(po::parse_command_line(argc, argv, desc), vm);
140 if (vm.count(
"help")) {
141 std::cout << desc << std::endl;
146 if (vm.count(
"prm-file")) {
147 prm.read_json(vm[
"prm-file"].as<string>());
150 if (vm.count(
"prm")) {
160 std::cout <<
"World size: " << world.size << std::endl;
162 const ptrdiff_t n = vm[
"size"].as<
int>();
163 const double h2i = (n - 1) * (n - 1);
165 boost::array<ptrdiff_t, 2> lo = { { 0, 0 } };
166 boost::array<ptrdiff_t, 2> hi = { { n - 1, n - 1 } };
168 prof.tic(
"partition");
169 DomainPartition<2> part(lo, hi, world.size);
170 ptrdiff_t chunk = part.size(world.rank);
172 std::vector<ptrdiff_t> domain(world.size + 1);
175 mpAllGather(world.m_message_passing_mng.get(), send_buf, receive_buf);
176 std::partial_sum(domain.begin(), domain.end(), domain.begin());
178 lo = part.domain(world.rank).min_corner();
179 hi = part.domain(world.rank).max_corner();
180 prof.toc(
"partition");
184 prof.tic(
"assemble");
185 std::vector<ptrdiff_t> ptr;
186 std::vector<ptrdiff_t> col;
187 std::vector<double> val;
188 std::vector<double> rhs;
190 ptr.reserve(chunk + 1);
191 col.reserve(chunk * 5);
192 val.reserve(chunk * 5);
196 for (ptrdiff_t j = lo[1]; j <= hi[1]; ++j) {
197 for (ptrdiff_t i = lo[0]; i <= hi[0]; ++i) {
199 col.push_back(renum(i, j - 1));
204 col.push_back(renum(i - 1, j));
208 col.push_back(renum(i, j));
209 val.push_back(4 * h2i);
212 col.push_back(renum(i + 1, j));
217 col.push_back(renum(i, j + 1));
221 ptr.push_back(col.size());
224 prof.toc(
"assemble");
226 bool single_level = vm[
"single-level"].as<
bool>();
229 prm.put(
"precond.class",
"relaxation");
231 Alina::SolverResult r = solve<Alina::PreconditionerRuntime>(context, world, prm, std::tie(chunk, ptr, col, val));
233 if (world.rank == 0) {
234 std::cout <<
"Iterations: " << r.nbIteration() << std::endl
235 <<
"Error: " << r.residual() << std::endl
237 << prof << std::endl;
Iterative solver wrapper for distributed linear systems.
Class to store parameters as a hierarchical key/value tree.
Modifiable view of an array of type T.
Constant view of an array of type T.
Matrix class, to be used by user.
static Ref< IMessagePassingMng > createAndInitializeMPI(int *argc, char **argv[], int wanted_thread_level)
Initialize MPI and creates a manager associated with the MPI_WORLD_COMM communicator.
Describes a set of command-line options.
Stores parsed option values.
void mpAllGather(IMessagePassingMng *pm, const ISerializer *send_serializer, ISerializer *receive_serialize)
allGather() message for serialization
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Convenience wrapper around MPI_Comm.