Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
poisson3Db_mpi.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/*
9The MIT License
10
11Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19
20The above copyright notice and this permission notice shall be included in
21all copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29THE SOFTWARE.
30*/
31
32#include <vector>
33#include <iostream>
34
35#include "arccore/alina/BuiltinBackend.h"
36#include "arccore/alina/Adapters.h"
37
38#include "arccore/alina/DistributedMatrix.h"
39#include "arccore/alina/DistributedPreconditionedSolver.h"
40#include "arccore/alina/DistributedAMG.h"
41#include "arccore/alina/DistributedCoarsening.h"
42#include "arccore/alina/DistributedRelaxation.h"
43#include "arccore/alina/DistributedSolver.h"
44
45#include "arccore/alina/IO.h"
46#include "arccore/alina/Profiler.h"
47
48#if defined(ARCCORE_ALINA_HAVE_PARMETIS)
49#include "arccore/alina/ParmetisMatrixPartitioner.h"
50#endif
51
52using namespace Arcane;
53using namespace Arcane::Alina;
54
55//---------------------------------------------------------------------------
56int main(int argc, char* argv[])
57{
58 // The matrix and the RHS file names should be in the command line options:
59 if (argc < 3) {
60 std::cerr << "Usage: " << argv[0] << " <matrix.bin> <rhs.bin>" << std::endl;
61 return 1;
62 }
63
64 Alina::mpi_init mpi(&argc, &argv);
65 Alina::mpi_communicator world(MPI_COMM_WORLD);
66
67 auto& prof = Alina::Profiler::globalProfiler();
68
69 // Read the system matrix and the RHS:
70 prof.tic("read");
71 // Get the global size of the matrix:
72 ptrdiff_t rows = Alina::IO::crs_size<ptrdiff_t>(argv[1]);
73 ptrdiff_t cols;
74
75 // Split the matrix into approximately equal chunks of rows
76 ptrdiff_t chunk = (rows + world.size - 1) / world.size;
77 ptrdiff_t row_beg = std::min(rows, chunk * world.rank);
78 ptrdiff_t row_end = std::min(rows, row_beg + chunk);
79 chunk = row_end - row_beg;
80
81 // Read our part of the system matrix and the RHS.
82 std::vector<ptrdiff_t> ptr, col;
83 std::vector<double> val, rhs;
84 Alina::IO::read_crs(argv[1], rows, ptr, col, val, row_beg, row_end);
85 Alina::IO::read_dense(argv[2], rows, cols, rhs, row_beg, row_end);
86 prof.toc("read");
87
88 if (world.rank == 0)
89 std::cout
90 << "World size: " << world.size << std::endl
91 << "Matrix " << argv[1] << ": " << rows << "x" << rows << std::endl
92 << "RHS " << argv[2] << ": " << rows << "x" << cols << std::endl;
93
94 // Compose the solver type
95 typedef BuiltinBackend<double> DBackend;
96 typedef BuiltinBackend<float> FBackend;
98 FBackend,
102
103 // Create the distributed matrix from the local parts.
104 auto A = std::make_shared<DistributedMatrix<DBackend>>(world, std::tie(chunk, ptr, col, val));
105
106 // Partition the matrix and the RHS vector.
107 // If neither ParMETIS not PT-SCOTCH are not available,
108 // just keep the current naive partitioning.
109#if defined(ARCCORE_ALINA_HAVE_PARMETIS)
111
112 if (world.size > 1) {
113 prof.tic("partition");
114 Partition part;
115
116 // part(A) returns the distributed permutation matrix:
117 auto P = part(*A);
118 auto R = transpose(*P);
119
120 // Reorder the matrix:
121 A = product(*R, *product(*A, *P));
122
123 // and the RHS vector:
124 std::vector<double> new_rhs(R->loc_rows());
125 R->move_to_backend(typename DBackend::params());
126 Alina::backend::spmv(1, *R, rhs, 0, new_rhs);
127 rhs.swap(new_rhs);
128
129 // Update the number of the local rows
130 // (it may have changed as a result of permutation):
131 chunk = A->loc_rows();
132 prof.toc("partition");
133 }
134#endif
135
136 // Initialize the solver:
137 prof.tic("setup");
138 Solver solve(world, A);
139 prof.toc("setup");
140
141 // Show the mini-report on the constructed solver:
142 if (world.rank == 0)
143 std::cout << solve << std::endl;
144
145 // Solve the system with the zero initial approximation:
146 std::vector<double> x(chunk, 0.0);
147
148 prof.tic("solve");
149 Alina::SolverResult r = solve(*A, rhs, x);
150 prof.toc("solve");
151
152 // Output the number of iterations, the relative error,
153 // and the profiling data:
154 if (world.rank == 0)
155 std::cout << "Iters: " << r.nbIteration() << std::endl
156 << "Error: " << r.residual() << std::endl
157 << prof << std::endl;
158}
Iterative solver wrapper for distributed linear systems.
Result of a solving.
Definition AlinaUtils.h:52
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Distributed smoothed aggregation coarsening scheme.
Convenience wrapper around MPI_Comm.
Convenience wrapper around MPI_Init/MPI_Finalize.