Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_HypreMLABecLap.H
Go to the documentation of this file.
1#ifndef AMREX_HYPRE_ML_ABECLAP_H_
2#define AMREX_HYPRE_ML_ABECLAP_H_
3#include <AMReX_Config.H>
4
6#include <AMReX_Geometry.H>
8#include <AMReX_LO_BCTYPES.H>
9#include <AMReX_MLMGBndry.H>
10#include <AMReX_MultiFab.H>
11#include <AMReX_MultiMask.H>
12
13#include <HYPRE_sstruct_ls.h>
14
15#include <limits>
16#include <unordered_map>
17#include <utility>
18
25
26namespace amrex {
27
28enum struct HypreSolverID {
30};
31
32// single component only, cell centered only
33
38{
39public:
40 HypreMLABecLap (HypreMLABecLap const&) = delete;
44
55 Vector<BoxArray> a_grids,
57 HypreSolverID a_hypre_solver_id,
58 std::string a_parmparse_prefix = "hypre_mlabeclap");
59
61
63 void setVerbose (int v) { m_verbose = v; }
65 void setMaxIter (int v) { m_maxiter = v; }
67 void setIsSingular (bool v) { m_is_singular = v; }
68
75 void setReuseSolverSetup (bool v) { m_reuse_setup = v; }
79 void setReuseSetupMaxIter (int n) noexcept { m_reuse_max_iter = n; }
82 if (m_ij_solver) { m_ij_solver->setNeedSetup(); }
83 m_algmg_reset = true;
84 }
85
86 void setThrowException (bool t) noexcept { m_throw_exception = t; }
88 [[nodiscard]] int getNumIters () const noexcept { return m_num_iters; }
89 [[nodiscard]] Real getFinalResidualNorm () const noexcept { return m_final_res; }
90
103 void setup (Real a_ascalar, Real a_bscalar,
104 Vector<MultiFab const*> const& a_acoefs,
108 Vector<MultiFab const*> const& a_levelbcdata,
109 std::pair<MultiFab const*, IntVect> const& a_coarse_bc = {nullptr, IntVect(0)});
110
119 void solve (Vector<MultiFab*> const& a_sol, Vector<MultiFab const*> const& a_rhs,
120 Real a_reltol, Real a_abstol);
121
122 // update? updateDirichleBC? or updateCoeffs?
123
124 // public for cuda
125
132 void commBCoefs (int flev, Array<MultiFab const*,AMREX_SPACEDIM> const& a_bcoefs);
140 void commBCoefs_local (int flev, Array<MultiFab const*,AMREX_SPACEDIM> const& a_bcoefs,
141 Vector<FabArrayBase::CopyComTag> const& tags);
142
144 void buildIJMaps ();
145
152 void updateIJValues (Gpu::DeviceVector<Real> const& vals);
153
155 void solveIJ (Vector<MultiFab*> const& a_sol, Vector<MultiFab const*> const& a_rhs,
156 Real a_reltol, Real a_abstol);
157
158private:
159
160 void addNonStencilEntriesToGraph ();
161
164 [[nodiscard]] Long cellId (int lev, IntVect const& iv) const;
165
166 int m_verbose = 0;
167 int m_maxiter = 200;
168 bool m_is_singular = false;
169
170 Vector<Geometry> m_geom;
171 Vector<BoxArray> m_grids;
172 Vector<DistributionMapping> m_dmap;
173 std::string m_parmparse_prefix;
174 int m_nlevels = 0;
175 MPI_Comm m_comm = MPI_COMM_NULL;
176
177 Vector<IntVect> m_ref_ratio;
178
179 Real m_ascalar = std::numeric_limits<Real>::max();
180 Real m_bscalar = std::numeric_limits<Real>::max();
187
188 Vector<std::unique_ptr<MLMGBndry>> m_bndry;
189 Vector<std::unique_ptr<BndryRegister>> m_bndry_rhs;
190 Vector<iMultiFab> m_fine_masks;
191 Vector<iMultiFab> m_crse_masks;
192
193 // For coarse cells at coarse/fine interface. The vector is for AMR
194 // levels.
195 Vector<iMultiFab> m_c2f_offset_from; // offset for sparse coarse from-cells
196 Vector<LayoutData<int>> m_c2f_total_from; // # of coarse from-cells w/ c2f entries
197 Vector<iMultiFab> m_c2f_nentries; // # of non-stencil entries
198 Vector<iMultiFab> m_c2f_offset_to; // offset for sparse to-cells, including fine (and coarse in 3d) cells
199 Vector<LayoutData<int>> m_c2f_total_to; // total sum of non-stencil entries in a Box
200
201 // B coefficients at coarse/fine interface
202 Vector<Array<iMultiFab,AMREX_SPACEDIM>> m_offset_cf_bcoefs;
203 Vector<Array<LayoutData<std::unique_ptr<Gpu::DeviceVector<Real>>>,AMREX_SPACEDIM>> m_cf_bcoefs;
204
205#ifdef AMREX_USE_GPU
206 template <class T> using HostVector = Gpu::PinnedVector<T>;
207#else
208 template <class T> using HostVector = Vector<T>;
209#endif
210
211 // For fine cells at coarse/fine interface. The non-stencil entries are
212 // from fine to coarse. The outer vector is for AMR levels.
213 Vector<HostVector<int>> m_f2c_bno; // local box number
214 Vector<HostVector<IntVect>> m_f2c_cell; // fine cell
215 Vector<Vector<HYPRE_Int>> m_f2c_nentries; // # of non-stencil entries
216 Vector<HostVector<std::size_t>> m_f2c_offset; // offset into m_f2c_values
217 Vector<HostVector<Real>> m_f2c_values; // values for non-stencil entries
218
219 HYPRE_SStructGrid m_ss_grid = nullptr;
220 HYPRE_SStructStencil m_ss_stencil = nullptr;
221 HYPRE_SStructGraph m_ss_graph = nullptr;
222 HYPRE_SStructSolver m_ss_solver = nullptr;
223 HYPRE_SStructSolver m_ss_precond = nullptr;
224 HYPRE_SStructMatrix m_ss_A = nullptr;
225 HYPRE_SStructVector m_ss_x = nullptr;
226 HYPRE_SStructVector m_ss_b = nullptr;
227
228 HYPRE_Solver m_solver = nullptr;
229
232 std::unique_ptr<HypreIJIface> m_ij_solver;
233
236 bool m_use_ij = false;
237 bool m_reuse_setup = false;
238 bool m_throw_exception = false;
240 bool m_ij_vectors_assembled = false;
241 bool m_ij_gather_failed = false; // hypre dropped an entry: no in-place updates
243 bool m_algmg_reset = false;
245 bool m_algmg_kept = false;
246 int m_reuse_max_iter = 0;
247 Vector<Vector<Long>> m_box_offset; // first row id of each box, per level
248 Long m_ij_lower = 0;
249 Long m_ij_upper = -1;
251 std::unordered_map<Long,Vector<Long>> m_ns_targets;
255 Vector<std::unique_ptr<FabArray<BaseFab<Long>>>> m_cell_id;
256 Vector<LayoutData<Gpu::DeviceVector<HYPRE_BigInt>>> m_c2f_targets;
257 Vector<Gpu::DeviceVector<HYPRE_BigInt>> m_f2c_rows;
258 Vector<Gpu::DeviceVector<HYPRE_BigInt>> m_f2c_cols;
259 Gpu::DeviceVector<HYPRE_BigInt> m_ij_rowids; // the local rows
264 struct EntryGather
265 {
268 [[nodiscard]] bool empty () const { return offset.empty(); }
269 [[nodiscard]] Long numTriplets () const { return Long(trip.size()); }
270 void clear () { offset.clear(); trip.clear(); }
271 };
274 EntryGather m_ij_gather;
275
279 enum class Backend { hypre, algmg };
280 Backend m_backend = Backend::hypre;
281 int m_num_iters = 0; // last solve (IJ mode)
282 Real m_final_res = 0.0;
283 Vector<Long> m_rank_offset; // first row of each rank, plus the total
284 composite::OwnedCSR m_csr; // owned rows (AlgMG backend, dumps)
285 std::unique_ptr<SpMatrix<Real>> m_spA;
286 std::unique_ptr<AlgMG<Real>> m_algmg;
289 Gpu::DeviceVector<Long> m_alg_row_offset;
290 Gpu::DeviceVector<Long> m_alg_col_index;
291 EntryGather m_alg_gather;
292
300 int m_dump_max = 0;
301 std::string m_dump_prefix;
302 Long m_dump_gather_max = 50000000;
303 void dumpSystem (int k, Gpu::DeviceVector<Real> const& v, bool solution);
305 void fail (std::string const& msg) const;
306
307 HypreSolverID m_hypre_solver_id = HypreSolverID::BoomerAMG;
308 HYPRE_Int m_hypre_object_type = HYPRE_PARCSR;
309};
310
311}
312
313#endif
Problem-domain geometry: maps between index space and physical space.
Constants describing linear-operator boundary condition types.
MultiFab-style container that stores Mask objects per grid.
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
void solveIJ(Vector< MultiFab * > const &a_sol, Vector< MultiFab const * > const &a_rhs, Real a_reltol, Real a_abstol)
Solve with the IJ system (see m_use_ij); public for cuda.
Definition AMReX_HypreMLABecLap.cpp:1751
void solve(Vector< MultiFab * > const &a_sol, Vector< MultiFab const * > const &a_rhs, Real a_reltol, Real a_abstol)
Solve the assembled system with HYPRE (single-component).
Definition AMReX_HypreMLABecLap.cpp:1310
void setMaxIter(int v)
Override the maximum number of HYPRE iterations (v).
Definition AMReX_HypreMLABecLap.H:65
void setReuseSolverSetup(bool v)
IJ mode only: let setup() be called again with new coefficients (same grids), updating the matrix val...
Definition AMReX_HypreMLABecLap.H:75
HypreMLABecLap(HypreMLABecLap const &)=delete
void commBCoefs_local(int flev, Array< MultiFab const *, 3 > const &a_bcoefs, Vector< FabArrayBase::CopyComTag > const &tags)
Local portion of commBCoefs used to issue device-friendly copies.
Definition AMReX_HypreMLABecLap.cpp:2358
void resetSolverSetup()
IJ mode only: redo the solver/preconditioner setup at the next solve.
Definition AMReX_HypreMLABecLap.H:81
void setVerbose(int v)
Set HYPRE's logging verbosity (v).
Definition AMReX_HypreMLABecLap.H:63
~HypreMLABecLap()
Definition AMReX_HypreMLABecLap.cpp:274
void commBCoefs(int flev, Array< MultiFab const *, 3 > const &a_bcoefs)
Communicate b coefficients needed by level flev at coarse/fine interfaces.
Definition AMReX_HypreMLABecLap.cpp:2031
int getNumIters() const noexcept
Iterations and final relative residual of the last solve.
Definition AMReX_HypreMLABecLap.H:88
void setReuseSetupMaxIter(int n) noexcept
Definition AMReX_HypreMLABecLap.H:79
void setIsSingular(bool v)
Flag whether the assembled operator is singular (v).
Definition AMReX_HypreMLABecLap.H:67
void setup(Real a_ascalar, Real a_bscalar, Vector< MultiFab const * > const &a_acoefs, Vector< Array< MultiFab const *, 3 > > const &a_bcoefs, Array< LinOpBCType, 3 > const &a_lobc, Array< LinOpBCType, 3 > const &a_hibc, Vector< MultiFab const * > const &a_levelbcdata, std::pair< MultiFab const *, IntVect > const &a_coarse_bc={nullptr, IntVect(0)})
Assemble the HYPRE operators/storage for the provided coefficients.
Definition AMReX_HypreMLABecLap.cpp:612
HypreMLABecLap(HypreMLABecLap &&)=delete
void buildIJMaps()
Build the device maps of the IJ assembly; public for cuda.
Definition AMReX_HypreMLABecLap.cpp:1546
Real getFinalResidualNorm() const noexcept
Definition AMReX_HypreMLABecLap.H:89
void setThrowException(bool t) noexcept
Throw std::runtime_error instead of aborting when the solve fails.
Definition AMReX_HypreMLABecLap.H:86
void buildIJSlots(Gpu::DeviceVector< HYPRE_BigInt > const &rows, Gpu::DeviceVector< HYPRE_BigInt > const &cols)
Definition AMReX_HypreMLABecLap.cpp:1644
void updateIJValues(Gpu::DeviceVector< Real > const &vals)
Definition AMReX_HypreMLABecLap.cpp:1720
HypreMLABecLap & operator=(HypreMLABecLap const &)=delete
bool empty() const noexcept
Definition AMReX_PODVector.H:658
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
PODVector< T, PinnedArenaAllocator< T > > PinnedVector
A PODVector that uses the pinned memory arena.
Definition AMReX_GpuContainers.H:48
std::array< T, N > Array
Definition AMReX_Array.H:31
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
int MPI_Comm
Definition AMReX_ccse-mpi.H:51
static constexpr int MPI_COMM_NULL
Definition AMReX_ccse-mpi.H:59
Definition AMReX_Amr.cpp:50
@ bogus
Definition AMReX_LO_BCTYPES.H:39
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
HypreSolverID
Definition AMReX_HypreMLABecLap.H:28
@ BoomerAMG
Definition AMReX_HypreMLABecLap.H:29
@ SSAMG
Definition AMReX_HypreMLABecLap.H:29