7#ifndef AMREX_HYPRE_SOLVER_H_
8#define AMREX_HYPRE_SOLVER_H_
16#include "_hypre_utilities.h"
62 template <
class Marker,
class Filler>
71 std::string a_options_namespace =
"hypre");
73 template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
74 std::is_same_v<
typename MF::value_type,
75 HYPRE_Real>,
int> = 0>
88 HYPRE_Real rel_tol, HYPRE_Real abs_tol,
int max_iter);
102 template <
class Marker>
104 std::enable_if_t<IsCallable<Marker,int,int,int,int,int>::value>
106 std::enable_if_t<IsCallableR<bool,Marker,int,int,int,int,int>::value>
110 template <
typename AI>
113 template <
class Filler,
114 std::enable_if_t<
IsCallable<Filler,int,int,int,int,int,
116 HYPRE_Int&, HYPRE_Int*,
117 HYPRE_Real*>::value,
int> FOO = 0>
120 template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
121 std::is_same_v<
typename MF::value_type,
122 HYPRE_Real>,
int> = 0>
126 template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
127 std::is_same_v<
typename MF::value_type,
128 HYPRE_Real>,
int> = 0>
162 HYPRE_IJMatrix
m_A =
nullptr;
163 HYPRE_IJVector
m_b =
nullptr;
164 HYPRE_IJVector
m_x =
nullptr;
168template <
class Marker,
class Filler>
177 std::string a_options_namespace)
178 : m_nvars (int(a_index_type.size())),
179 m_index_type (a_index_type),
183 m_verbose (a_verbose),
184 m_options_namespace(std::move(a_options_namespace))
198 for (
int ivar = 0; ivar <
m_nvars; ++ivar) {
204 nrows_max +=
m_grids[ivar].numPts();
209 "Need to configure Hypre with --enable-bigint");
212 for (
int ivar = 0; ivar <
m_nvars; ++ivar) {
234 if (nrows_allprocs.
size() > 1) {
235 MPI_Allgather(&
m_nrows_proc,
sizeof(HYPRE_Int), MPI_CHAR,
236 nrows_allprocs.data(),
sizeof(HYPRE_Int), MPI_CHAR,
m_comm);
243 HYPRE_Int proc_begin = 0;
244 for (
int i = 0; i < myproc; ++i) {
245 proc_begin += nrows_allprocs[i];
248 HYPRE_Int proc_end = proc_begin;
250 for (
int ivar = 0; ivar <
m_nvars; ++ivar) {
262 using AtomicInt = std::conditional_t<
sizeof(HYPRE_Int) == 4,
263 HYPRE_Int,
unsigned long long>;
264 fill_global_id<AtomicInt>();
267 HYPRE_Int ilower = proc_begin;
268 HYPRE_Int iupper = proc_end-1;
281template <
class Marker>
283 std::enable_if_t<IsCallable<Marker,int,int,int,int,int>::value>
285 std::enable_if_t<IsCallableR<bool,Marker,int,int,int,int,int>::value>
294 int boxno = mfi.LocalIndex();
296 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
300 m_cell_offset[mfi].resize(npts_tot);
302 int* p_cell_offset = m_cell_offset[mfi].data();
303 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
305 auto const& lid = m_local_id[ivar].array(mfi);
306 auto const& owner = m_owner_mask[ivar]->const_array(mfi);
308 const auto npts =
static_cast<int>(bx.
numPts());
309 int npts_box = amrex::Scan::PrefixSum<int>(npts,
313 int id = (owner ( cell.
x,cell.
y,cell.
z ) &&
314 marker(boxno,cell.
x,cell.
y,cell.
z,ivar)) ? 1 : 0;
315 lid(cell.
x,cell.
y,cell.
z) = id;
321 if (lid(cell.
x,cell.
y,cell.
z)) {
322 lid(cell.
x,cell.
y,cell.
z) = ps;
323 p_cell_offset[ps] =
offset;
325 lid(cell.
x,cell.
y,cell.
z) = std::numeric_limits<int>::lowest();
329 m_nrows_grid[ivar][mfi] = npts_box;
330 npts_tot += npts_box;
331 p_cell_offset += npts_box;
333 m_cell_offset[mfi].resize(npts_tot);
342 int boxno = mfi.LocalIndex();
343 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
345 auto const& lid = m_local_id[ivar].array(mfi);
346 auto const& owner = m_owner_mask[ivar]->const_array(mfi);
350 for (
int k = lo.z; k <= hi.z; ++k) {
351 for (
int j = lo.y; j <= hi.y; ++j) {
352 for (
int i = lo.x; i <= hi.x; ++i) {
353 if (owner(i,j,k) && marker(boxno,i,j,k,ivar)) {
356 lid(i,j,k) = std::numeric_limits<int>::lowest();
359 m_nrows_grid[ivar][mfi] = id;
367 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
368 nrows += m_nrows_grid[ivar][mfi];
370 m_nrows[mfi] = nrows;
371 m_nrows_proc += nrows;
376template <
typename AI>
385 if constexpr (std::is_same_v<HYPRE_Int,AI>) {
386 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
387 p_global_id.push_back(&(m_global_id[ivar]));
390 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
391 global_id_raii.emplace_back(m_global_id[ivar].
boxArray(),
392 m_global_id[ivar].DistributionMap(),
393 1, m_global_id[ivar].nGrowVect());
394 p_global_id.push_back(&(global_id_raii[ivar]));
399#pragma omp parallel if (Gpu::notInLaunchRegion())
402 auto& rows_vec = m_global_id_vec[mfi];
403 rows_vec.resize(m_nrows[mfi]);
406 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
407 HYPRE_Int
const os = m_id_offset[ivar][mfi];
408 Box bx = mfi.validbox();
410 Array4<AI> const& gid = p_global_id[ivar]->array(mfi);
411 auto const& lid = m_local_id[ivar].const_array(mfi);
412 HYPRE_Int* rows = rows_vec.data() + nrows;
413 nrows += m_nrows_grid[ivar][mfi];
416 if (lid.contains(i,j,k) && lid(i,j,k) >= 0) {
417 const auto id = lid(i,j,k) + os;
418 rows[lid(i,j,k)] = id;
419 gid(i,j,k) =
static_cast<AI
>(id);
421 gid(i,j,k) =
static_cast<AI
>
422 (std::numeric_limits<HYPRE_Int>::max());
428 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
430 m_geom.periodicity());
431 p_global_id[ivar]->FillBoundary(m_geom.periodicity());
433 if constexpr (!std::is_same<HYPRE_Int, AI>()) {
434 auto const& dst = m_global_id[ivar].arrays();
435 auto const& src = p_global_id[ivar]->const_arrays();
439 dst[b](i,j,k) =
static_cast<HYPRE_Int
>(src[b](i,j,k));
454 auto* p_cols_tmp = cols_tmp.
data();
455 auto* p_mat_tmp = mat_tmp.
data();
456 auto const* p_cols = cols.
data();
457 auto const* p_mat = mat.
data();
459 Scan::PrefixSum<T>(N,
462 return static_cast<T
>(p_cols[i] >= 0);
466 if (p_cols[i] >= 0) {
467 p_cols_tmp[s] = p_cols[i];
468 p_mat_tmp[s] = p_mat[i];
471 Scan::Type::exclusive, Scan::noRetSum);
472 std::swap(cols_tmp, cols);
473 std::swap(mat_tmp, mat);
480template <
class Filler,
481 std::enable_if_t<IsCallable<Filler,int,int,int,int,int,
482 Array4<HYPRE_Int const>
const*,
483 HYPRE_Int&, HYPRE_Int*,
484 HYPRE_Real*>::value,
int> FOO>
496 for (
MFIter mfi(m_local_id[0],mfitinfo); mfi.
isValid(); ++mfi)
498 int boxno = mfi.LocalIndex();
499 const HYPRE_Int nrows = m_nrows[mfi];
504 HYPRE_Int* ncols = ncols_vec.
data();
508 HYPRE_Int* cols = cols_vec.
data();
512 HYPRE_Real* mat = mat_vec.
data();
515 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
516 gid_v[ivar] = m_global_id[ivar].const_array(mfi);
521 (gid_v.data(), gid_v.
size());
522 auto const* pgid = gid_buf.
data();
523 auto const* p_cell_offset = m_cell_offset[mfi].data();
525 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
526 const HYPRE_Int nrows_var = m_nrows_grid[ivar][mfi];
529 ntot += Reduce::Sum<Long>(nrows_var,
533 filler(boxno, cell.
x, cell.
y, cell.
z, ivar, pgid,
538 p_cell_offset += nrows_var;
540 cols +=
Long(nrows_var)*MSS;
541 mat +=
Long(nrows_var)*MSS;
546 if (ntot >=
Long(std::numeric_limits<int>::max())) {
547 detail::pack_matrix_gpu<Long>(cols_tmp, mat_tmp, cols_vec, mat_vec);
549 detail::pack_matrix_gpu<int>(cols_tmp, mat_tmp, cols_vec, mat_vec);
552 auto* pgid = gid_v.data();
553 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
554 if (m_nrows_grid[ivar][mfi] > 0) {
555 auto const& lid = m_local_id[ivar].const_array(mfi);
557 [=,&ncols,&cols,&mat] (
int i,
int j,
int k)
559 if (lid(i,j,k) >= 0) {
560 filler(boxno, i, j, k, ivar, pgid, *ncols, cols, mat);
570 const auto& rows_vec = m_global_id_vec[mfi];
571 HYPRE_Int
const* rows = rows_vec.data();
574 HYPRE_IJMatrixSetValues(m_A, nrows, ncols_vec.
data(), rows,
576 Gpu::hypreSynchronize();
579 HYPRE_IJMatrixAssemble(m_A);
583template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
584 std::is_same_v<
typename MF::value_type,
585 HYPRE_Real>,
int> FOO>
589 HYPRE_Real rel_tol, HYPRE_Real abs_tol,
int max_iter)
595 HYPRE_IJVectorInitialize(m_b);
596 HYPRE_IJVectorInitialize(m_x);
598 load_vectors(a_soln, a_rhs);
600 HYPRE_IJVectorAssemble(m_x);
601 HYPRE_IJVectorAssemble(m_b);
603 m_hypre_ij->solve(rel_tol, abs_tol, max_iter);
605 get_solution(a_soln);
609template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
610 std::is_same_v<
typename MF::value_type,
611 HYPRE_Real>,
int> FOO>
625 const HYPRE_Int nrows = m_nrows[mfi];
632 auto* xp = xvec.
data();
633 auto* bp = bvec.
data();
635 HYPRE_Int
const* rows = m_global_id_vec[mfi].data();
638 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
639 if (m_nrows_grid[ivar][mfi] > 0) {
640 auto const& xfab = a_soln[ivar]->const_array(mfi);
641 auto const& bfab = a_rhs [ivar]->const_array(mfi);
642 auto const& lid = m_local_id[ivar].const_array(mfi);
644 HYPRE_Real* b = bp +
offset;
648 if (lid(i,j,k) >= 0) {
649 x[lid(i,j,k)] = xfab(i,j,k);
650 b[lid(i,j,k)] = bfab(i,j,k);
653 offset += m_nrows_grid[ivar][mfi];
657 Gpu::streamSynchronize();
658 HYPRE_IJVectorSetValues(m_x, nrows, rows, xp);
659 HYPRE_IJVectorSetValues(m_b, nrows, rows, bp);
660 Gpu::hypreSynchronize();
666template <class MF, std::enable_if_t<IsFabArray<MF>::value &&
667 std::is_same_v<
typename MF::value_type,
668 HYPRE_Real>,
int> FOO>
680 const HYPRE_Int nrows = m_nrows[mfi];
685 auto* xp = xvec.
data();
687 HYPRE_Int
const* rows = m_global_id_vec[mfi].data();
689 HYPRE_IJVectorGetValues(m_x, nrows, rows, xp);
690 Gpu::hypreSynchronize();
693 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
694 if (m_nrows_grid[ivar][mfi] > 0) {
695 auto const& xfab = a_soln[ivar]->array(mfi);
696 auto const& lid = m_local_id[ivar].const_array(mfi);
701 if (lid(i,j,k) >= 0) {
702 xfab(i,j,k) =
x[lid(i,j,k)];
705 offset += m_nrows_grid[ivar][mfi];
708 Gpu::streamSynchronize();
712 for (
int ivar = 0; ivar < m_nvars; ++ivar) {
714 m_geom.periodicity());
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1089
A collection of Boxes stored in an Array.
Definition AMReX_BoxArray.H:567
__host__ __device__ BoxND & grow(int i) noexcept
Definition AMReX_Box.H:641
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:356
__host__ __device__ BoxND & convert(IndexTypeND< dim > typ) noexcept
Convert the BoxND from the current type into the argument type. This may change the BoxND coordinates...
Definition AMReX_Box.H:974
__host__ __device__ IntVectND< dim > atOffset(Long offset) const noexcept
Given the offset, compute IntVectND<dim>
Definition AMReX_Box.H:1071
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:43
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:74
Periodicity periodicity() const noexcept
Definition AMReX_Geometry.H:356
Definition AMReX_GpuBuffer.H:18
T const * data() const noexcept
Definition AMReX_GpuBuffer.H:45
Solve Ax = b using HYPRE's generic IJ matrix format where A is a sparse matrix specified using the co...
Definition AMReX_HypreSolver.H:33
HypreSolver(Vector< IndexType > const &a_index_type, IntVect const &a_nghost, Geometry const &a_geom, BoxArray const &a_grids, DistributionMapping const &a_dmap, Marker &&a_marker, Filler &&a_filler, int a_verbose=0, std::string a_options_namespace="hypre")
Definition AMReX_HypreSolver.H:169
LayoutData< HYPRE_Int > m_nrows
Definition AMReX_HypreSolver.H:156
IntVect m_nghost
Definition AMReX_HypreSolver.H:135
Vector< LayoutData< HYPRE_Int > > m_nrows_grid
Definition AMReX_HypreSolver.H:154
int getNumIters() const
Definition AMReX_HypreSolver.H:90
Geometry m_geom
Definition AMReX_HypreSolver.H:136
LayoutData< Gpu::DeviceVector< HYPRE_Int > > m_global_id_vec
Definition AMReX_HypreSolver.H:148
std::enable_if_t< IsCallable< Marker, int, int, int, int, int >::value > fill_local_id(Marker const &marker)
Definition AMReX_HypreSolver.H:287
std::unique_ptr< HypreIJIface > m_hypre_ij
Definition AMReX_HypreSolver.H:159
int m_nvars
Definition AMReX_HypreSolver.H:133
Vector< LayoutData< HYPRE_Int > > m_id_offset
Definition AMReX_HypreSolver.H:155
MPI_Comm m_comm
Definition AMReX_HypreSolver.H:143
void get_solution(Vector< MF * > const &a_soln)
Definition AMReX_HypreSolver.H:670
HYPRE_IJVector getx() const
Definition AMReX_HypreSolver.H:98
void load_vectors(Vector< MF * > const &a_soln, Vector< MF const * > const &a_rhs)
Definition AMReX_HypreSolver.H:613
HYPRE_IJMatrix getA() const
Definition AMReX_HypreSolver.H:96
std::string m_options_namespace
Definition AMReX_HypreSolver.H:141
void fill_matrix(Filler const &filler)
Definition AMReX_HypreSolver.H:486
HYPRE_IJMatrix m_A
Definition AMReX_HypreSolver.H:162
void fill_global_id()
Definition AMReX_HypreSolver.H:378
void solve(Vector< MF * > const &a_soln, Vector< MF const * > const &a_rhs, HYPRE_Real rel_tol, HYPRE_Real abs_tol, int max_iter)
Definition AMReX_HypreSolver.H:587
HYPRE_Int m_nrows_proc
Definition AMReX_HypreSolver.H:157
LayoutData< Gpu::DeviceVector< int > > m_cell_offset
Definition AMReX_HypreSolver.H:151
Vector< FabArray< BaseFab< HYPRE_Int > > > m_global_id
Definition AMReX_HypreSolver.H:147
HYPRE_Real getFinalResidualNorm() const
Definition AMReX_HypreSolver.H:92
Vector< std::unique_ptr< iMultiFab > > m_owner_mask
Definition AMReX_HypreSolver.H:145
HYPRE_IJVector m_b
Definition AMReX_HypreSolver.H:163
Vector< IndexType > m_index_type
Definition AMReX_HypreSolver.H:134
Vector< BoxArray > m_grids
Definition AMReX_HypreSolver.H:137
Vector< iMultiFab > m_local_id
Definition AMReX_HypreSolver.H:146
int m_verbose
Definition AMReX_HypreSolver.H:140
DistributionMapping m_dmap
Definition AMReX_HypreSolver.H:138
HYPRE_IJVector m_x
Definition AMReX_HypreSolver.H:164
HYPRE_IJVector getb() const
Definition AMReX_HypreSolver.H:97
a one-thingy-per-box distributed object
Definition AMReX_LayoutData.H:13
void define(const BoxArray &a_grids, const DistributionMapping &a_dm)
Definition AMReX_LayoutData.H:25
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:63
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:147
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
size_type size() const noexcept
Definition AMReX_PODVector.H:648
void resize(size_type a_new_size, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_PODVector.H:728
void clear() noexcept
Definition AMReX_PODVector.H:646
T * data() noexcept
Definition AMReX_PODVector.H:666
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
Long size() const noexcept
Definition AMReX_Vector.H:53
amrex_long Long
Definition AMReX_INT.H:30
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:263
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
int NProcsSub() noexcept
number of ranks in current frame
Definition AMReX_ParallelContext.H:74
static constexpr struct amrex::Scan::Type::Exclusive exclusive
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:49
__host__ __device__ Dim3 ubound(Array4< T > const &a) noexcept
Definition AMReX_Array4.H:319
__host__ __device__ BoxND< dim > convert(const BoxND< dim > &b, const IntVectND< dim > &typ) noexcept
Return a BoxND with different type.
Definition AMReX_Box.H:1558
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:193
IntVect nGrowVect(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2859
void OverrideSync(FabArray< FAB > &fa, FabArray< IFAB > const &msk, const Periodicity &period)
Definition AMReX_FabArrayUtility.H:1442
std::unique_ptr< iMultiFab > OwnerMask(FabArrayBase const &mf, const Periodicity &period, const IntVect &ngrow)
Definition AMReX_iMultiFab.cpp:699
__host__ __device__ void Loop(Dim3 lo, Dim3 hi, F const &f) noexcept
Definition AMReX_Loop.H:127
BoxArray const & boxArray(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2864
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Definition AMReX_Array4.H:312
Definition AMReX_Array4.H:61
Definition AMReX_Dim3.H:12
int x
Definition AMReX_Dim3.H:12
int z
Definition AMReX_Dim3.H:12
int y
Definition AMReX_Dim3.H:12
Test if a given type T is callable with arguments of type Args...
Definition AMReX_TypeTraits.H:213
Definition AMReX_MFIter.H:20
MFItInfo & DisableDeviceSync() noexcept
Definition AMReX_MFIter.H:38
MFItInfo & UseDefaultStream() noexcept
Definition AMReX_MFIter.H:50