20 #ifndef OPM_HYPRE_SETUP_HPP 21 #define OPM_HYPRE_SETUP_HPP 23 #include <opm/simulators/linalg/PropertyTree.hpp> 24 #include <opm/simulators/linalg/gpuistl/detail/gpu_type_detection.hpp> 25 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreDataStructures.hpp> 26 #include <opm/simulators/linalg/gpuistl/hypreinterface/HypreErrorHandling.hpp> 28 #include <dune/istl/owneroverlapcopy.hh> 29 #include <dune/istl/paamg/graph.hh> 30 #include <dune/istl/paamg/pinfo.hh> 31 #include <dune/istl/repartition.hh> 35 #include <opm/simulators/linalg/gpuistl_hip/GpuSparseMatrixWrapper.hpp> 37 #include <opm/simulators/linalg/gpuistl/GpuSparseMatrixWrapper.hpp> 42 #include <HYPRE_parcsr_ls.h> 43 #include <_hypre_utilities.h> 53 template <
typename T,
bool ForceLegacy>
54 SparsityPattern setupSparsityPatternFromGpuMatrix(
const GpuSparseMatrixWrapper<T, ForceLegacy>& gpu_matrix,
55 const ParallelInfo& par_info,
58 template <
typename T,
bool ForceLegacy>
59 std::vector<HYPRE_Int> computeRowIndexesWithMappingGpu(
const GpuSparseMatrixWrapper<T, ForceLegacy>& gpu_matrix,
60 const std::vector<int>& local_dune_to_local_hypre);
66 template <
typename CommType,
typename MatrixType>
69 template <
typename MatrixType>
73 template <
typename MatrixType>
74 std::vector<HYPRE_Int> computeRowIndexesWithMappingCpu(
const MatrixType& matrix,
75 const std::vector<HYPRE_Int>& ncols,
76 const std::vector<int>& local_dune_to_local_hypre,
79 template <
typename MatrixType>
80 std::vector<HYPRE_Int> computeRowIndexesWithMappingCpu(
const MatrixType& matrix,
81 const std::vector<int>& local_dune_to_local_hypre);
92 #if HYPRE_USING_CUDA || HYPRE_USING_HIP 93 if (use_gpu_backend) {
94 OPM_HYPRE_SAFE_CALL(HYPRE_SetMemoryLocation(HYPRE_MEMORY_DEVICE));
95 OPM_HYPRE_SAFE_CALL(HYPRE_SetExecutionPolicy(HYPRE_EXEC_DEVICE));
97 OPM_HYPRE_SAFE_CALL(HYPRE_SetSpGemmUseVendor(
false));
99 OPM_HYPRE_SAFE_CALL(HYPRE_SetUseGpuRand(1));
100 OPM_HYPRE_SAFE_CALL(HYPRE_DeviceInitialize());
101 OPM_HYPRE_SAFE_CALL(HYPRE_PrintDeviceInfo());
105 OPM_HYPRE_SAFE_CALL(HYPRE_SetMemoryLocation(HYPRE_MEMORY_HOST));
106 OPM_HYPRE_SAFE_CALL(HYPRE_SetExecutionPolicy(HYPRE_EXEC_HOST));
120 OPM_HYPRE_SAFE_CALL(HYPRE_BoomerAMGCreate(&solver));
136 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetPrintLevel(solver, prm.
get<
int>(
"print_level", 0)));
137 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetMaxIter(solver, prm.
get<
int>(
"max_iter", 1)));
138 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetStrongThreshold(solver, prm.
get<
double>(
"strong_threshold", 0.5)));
139 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetAggTruncFactor(solver, prm.
get<
double>(
"agg_trunc_factor", 0.3)));
140 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetInterpType(solver, prm.
get<
int>(
"interp_type", 6)));
141 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetMaxLevels(solver, prm.
get<
int>(
"max_levels", 15)));
142 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetTol(solver, prm.
get<
double>(
"tolerance", 0.0)));
144 if (use_gpu_backend) {
145 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetRelaxType(solver, prm.
get<
int>(
"relax_type", 16)));
146 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetCoarsenType(solver, prm.
get<
int>(
"coarsen_type", 8)));
147 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetAggNumLevels(solver, prm.
get<
int>(
"agg_num_levels", 0)));
148 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetAggInterpType(solver, prm.
get<
int>(
"agg_interp_type", 6)));
150 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetKeepTranspose(solver,
true));
152 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetRelaxType(solver, prm.
get<
int>(
"relax_type", 13)));
153 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetCoarsenType(solver, prm.
get<
int>(
"coarsen_type", 10)));
154 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetAggNumLevels(solver, prm.
get<
int>(
"agg_num_levels", 1)));
155 HYPRE_SAFE_CALL(HYPRE_BoomerAMGSetAggInterpType(solver, prm.
get<
int>(
"agg_interp_type", 4)));
168 template <
typename CommType>
172 HYPRE_IJMatrix matrix;
174 if constexpr (std::is_same_v<CommType, Dune::Amg::SequentialInformation>) {
175 mpi_comm = MPI_COMM_SELF;
177 mpi_comm = comm.communicator();
180 HYPRE_IJMatrixCreate(mpi_comm, dof_offset, dof_offset + (N - 1), dof_offset, dof_offset + (N - 1), &matrix));
181 OPM_HYPRE_SAFE_CALL(HYPRE_IJMatrixSetObjectType(matrix, HYPRE_PARCSR));
182 OPM_HYPRE_SAFE_CALL(HYPRE_IJMatrixInitialize(matrix));
195 template <
typename CommType>
199 HYPRE_IJVector vector;
201 if constexpr (std::is_same_v<CommType, Dune::Amg::SequentialInformation>) {
202 mpi_comm = MPI_COMM_SELF;
204 mpi_comm = comm.communicator();
206 OPM_HYPRE_SAFE_CALL(HYPRE_IJVectorCreate(mpi_comm, dof_offset, dof_offset + (N - 1), &vector));
207 OPM_HYPRE_SAFE_CALL(HYPRE_IJVectorSetObjectType(vector, HYPRE_PARCSR));
208 OPM_HYPRE_SAFE_CALL(HYPRE_IJVectorInitialize(vector));
222 OPM_HYPRE_SAFE_CALL(HYPRE_BoomerAMGDestroy(solver));
236 OPM_HYPRE_SAFE_CALL(HYPRE_IJMatrixDestroy(matrix));
250 OPM_HYPRE_SAFE_CALL(HYPRE_IJVectorDestroy(vector));
261 template <
typename CommType,
typename MatrixType>
265 if constexpr (std::is_same_v<CommType, Dune::Amg::SequentialInformation>) {
361 template <
typename CommType,
typename MatrixType>
366 const auto& collective_comm = comm.communicator();
373 if (!(matrix.N() == comm.indexSet().size())) {
376 const_cast<CommType&
>(comm).buildGlobalLookup(matrix.N());
377 Dune::Amg::MatrixGraph<MatrixType> graph(const_cast<MatrixType&>(matrix));
378 Dune::fillIndexSetHoles(graph, const_cast<CommType&>(comm));
379 assert(matrix.N() == comm.indexSet().size());
385 for (
const auto& ind : comm.indexSet()) {
386 int local_ind = ind.local().local();
387 if (ind.local().attribute() == Dune::OwnerOverlapCopyAttributeSet::owner) {
399 bool owner_first =
true;
400 bool visited_ghost =
false;
401 std::size_t count = 0;
405 visited_ghost =
true;
413 owner_first = owner_first && !visited_ghost;
427 std::vector<HYPRE_Int> dof_counts_per_process(collective_comm.size());
428 collective_comm.allgather(&info.
N_owned, 1, dof_counts_per_process.data());
431 info.
dof_offset = std::accumulate(dof_counts_per_process.begin(),
432 dof_counts_per_process.begin() + collective_comm.rank(),
446 if (collective_comm.rank() > 0) {
465 template <
typename MatrixType>
469 #if HYPRE_USING_CUDA || HYPRE_USING_HIP 471 return setupSparsityPatternFromGpuMatrix(matrix, par_info, owner_first);
487 template <
typename MatrixType>
495 std::size_t cols_size = 0;
497 for (
auto row = matrix.begin(); row != matrix.end(); ++row) {
498 const int rowIdx = row.index();
500 cols_size += row->size();
503 pattern.
nnz = cols_size;
506 pattern.
nnz = matrix.nonzeroes();
512 pattern.
cols.resize(pattern.
nnz);
515 for (
auto row = matrix.begin(); row != matrix.end(); ++row) {
516 const int rind = row.index();
521 if (owner_first && local_rowIdx < 0) {
525 if (local_rowIdx >= 0) {
528 pattern.
rows[local_rowIdx] = global_rowIdx;
529 pattern.
ncols[local_rowIdx] = row->size();
533 for (
auto col = row->begin(); col != row->end(); ++col) {
535 assert(global_colIdx >= 0);
536 pattern.
cols[pos++] = global_colIdx;
543 #if HYPRE_USING_CUDA || HYPRE_USING_HIP 552 template <
typename T,
bool ForceLegacy>
562 std::size_t cols_size = 0;
564 auto host_row_ptrs = gpu_matrix.
getRowIndices().asStdVector();
565 for (
int rind = 0; rind < static_cast<int>(gpu_matrix.
N()); ++rind) {
567 const int row_start = host_row_ptrs[rind];
568 const int row_end = host_row_ptrs[rind + 1];
569 cols_size += (row_end - row_start);
572 pattern.
nnz = cols_size;
581 pattern.
cols.resize(pattern.
nnz);
584 auto host_row_ptrs = gpu_matrix.
getRowIndices().asStdVector();
588 for (
int rind = 0; rind < static_cast<int>(gpu_matrix.
N()); ++rind) {
593 if (owner_first && local_rowIdx < 0) {
597 const int row_start = host_row_ptrs[rind];
598 const int row_end = host_row_ptrs[rind + 1];
599 const int num_cols = row_end - row_start;
601 if (local_rowIdx >= 0) {
604 pattern.
rows[local_rowIdx] = global_rowIdx;
605 pattern.
ncols[local_rowIdx] = num_cols;
609 for (
int col_idx = row_start; col_idx < row_end; ++col_idx) {
610 const int colIdx = host_col_indices[col_idx];
612 assert(global_colIdx >= 0);
613 pattern.
cols[pos++] = global_colIdx;
619 #endif // HYPRE_USING_CUDA || HYPRE_USING_HIP 634 template <
typename MatrixType>
635 std::vector<HYPRE_Int>
637 const std::vector<HYPRE_Int>& ncols,
638 const std::vector<int>& local_dune_to_local_hypre,
643 std::vector<HYPRE_Int> row_indexes(ncols.size());
645 for (std::size_t i = 1; i < ncols.size(); ++i) {
646 row_indexes[i] = row_indexes[i - 1] + ncols[i - 1];
651 #if HYPRE_USING_CUDA || HYPRE_USING_HIP 653 return computeRowIndexesWithMappingGpu(matrix, local_dune_to_local_hypre);
655 #endif // HYPRE_USING_CUDA || HYPRE_USING_HIP 657 return computeRowIndexesWithMappingCpu(matrix, local_dune_to_local_hypre);
681 template <
typename MatrixType>
682 std::vector<HYPRE_Int>
683 computeRowIndexesWithMappingCpu(
const MatrixType& matrix,
const std::vector<int>& local_dune_to_local_hypre)
685 const int N = std::count_if(
686 local_dune_to_local_hypre.begin(), local_dune_to_local_hypre.end(), [](
int val) {
return val >= 0; });
687 std::vector<HYPRE_Int> row_indexes(N);
688 int data_position = 0;
690 for (
auto row = matrix.begin(); row != matrix.end(); ++row) {
691 const int dune_row_idx = row.index();
692 const int hypre_row_idx = local_dune_to_local_hypre[dune_row_idx];
694 if (hypre_row_idx >= 0) {
697 row_indexes[hypre_row_idx] = data_position;
700 data_position += row->size();
705 #if HYPRE_USING_CUDA || HYPRE_USING_HIP 717 template <
typename T,
bool ForceLegacy>
718 std::vector<HYPRE_Int>
720 const std::vector<int>& local_dune_to_local_hypre)
722 const int N = std::count_if(
723 local_dune_to_local_hypre.begin(), local_dune_to_local_hypre.end(), [](
int val) {
return val >= 0; });
724 std::vector<HYPRE_Int> row_indexes(N);
727 auto host_row_ptrs = gpu_matrix.
getRowIndices().asStdVector();
730 for (
int dune_row_idx = 0; dune_row_idx < static_cast<int>(gpu_matrix.
N()); ++dune_row_idx) {
731 const int hypre_row_idx = local_dune_to_local_hypre[dune_row_idx];
733 if (hypre_row_idx >= 0) {
736 row_indexes[hypre_row_idx] = host_row_ptrs[dune_row_idx];
742 #endif // HYPRE_USING_CUDA || HYPRE_USING_HIP 746 #endif // OPM_HYPRE_SETUP_HPP std::vector< int > local_dune_to_local_hypre
Mapping from local Dune indices to local HYPRE indices.
Definition: HypreDataStructures.hpp:44
Type trait to detect if a type is a GPU type.
Definition: gpu_type_detection.hpp:40
The GpuSparseMatrixWrapper Checks CUDA/HIP version and dispatches a version either using the old or t...
Definition: gpu_type_detection.hpp:32
T get(const std::string &key) const
Retrieve property value given hierarchical property key.
Definition: PropertyTree.cpp:59
std::size_t N() const
N returns the number of rows (which is equal to the number of columns)
Definition: GpuSparseMatrixWrapper.hpp:232
Unified interface for Hypre operations with both CPU and GPU data structures.
Definition: HypreCpuTransfers.hpp:29
GpuVector< int > & getRowIndices()
getRowIndices returns the row indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:271
HYPRE_Solver createAMGSolver()
Create Hypre BoomerAMG solver.
Definition: HypreSetup.hpp:117
std::size_t nonzeroes() const
nonzeroes behaves as the Dune::BCRSMatrix::nonzeros() function and returns the number of non zero blo...
Definition: GpuSparseMatrixWrapper.hpp:241
ParallelInfo setupHypreParallelInfoParallel(const CommType &comm, const MatrixType &matrix)
Create mappings between Dune and HYPRE indexing for parallel decomposition.
Definition: HypreSetup.hpp:363
void destroyMatrix(HYPRE_IJMatrix matrix)
Destroy Hypre matrix.
Definition: HypreSetup.hpp:233
void initialize([[maybe_unused]] bool use_gpu_backend)
Initialize the Hypre library and set memory/execution policy.
Definition: HypreSetup.hpp:89
HYPRE_IJMatrix createMatrix(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre matrix.
Definition: HypreSetup.hpp:170
HYPRE_Int nnz
Number of non-zero entries in matrix.
Definition: HypreDataStructures.hpp:97
HYPRE_Int dof_offset
Global index offset for this process's owned DOFs.
Definition: HypreDataStructures.hpp:69
bool owner_first
Whether owned DOFs appear first in local Dune ordering.
Definition: HypreDataStructures.hpp:77
std::vector< HYPRE_BigInt > rows
Global row indices for owned rows (size: N_owned)
Definition: HypreDataStructures.hpp:91
HYPRE_IJVector createVector(HYPRE_Int N, HYPRE_Int dof_offset, const CommType &comm)
Create Hypre vector.
Definition: HypreSetup.hpp:197
std::vector< int > local_dune_to_global_hypre
Mapping from local Dune indices to global HYPRE indices.
Definition: HypreDataStructures.hpp:51
ParallelInfo setupHypreParallelInfo(const CommType &comm, const MatrixType &matrix)
Setup parallel information for Hypre (automatically detects serial/parallel)
Definition: HypreSetup.hpp:263
SparsityPattern setupSparsityPatternFromCpuMatrix(const MatrixType &matrix, const ParallelInfo &par_info, bool owner_first)
Setup sparsity pattern from CPU matrix (BCRSMatrix)
Definition: HypreSetup.hpp:489
HYPRE_Int N_owned
Number of DOFs owned by this MPI process.
Definition: HypreDataStructures.hpp:62
ParallelInfo setupHypreParallelInfoSerial(HYPRE_Int N)
Setup parallel information for Hypre in serial case.
Definition: HypreSetup.hpp:279
std::vector< HYPRE_Int > ncols
Non-zero entries per owned row (size: N_owned)
Definition: HypreDataStructures.hpp:88
SparsityPattern setupSparsityPattern(const MatrixType &matrix, const ParallelInfo &par_info, bool owner_first)
Setup sparsity pattern from matrix (automatically detects CPU/GPU type)
Definition: HypreSetup.hpp:467
std::vector< int > local_hypre_to_local_dune
Mapping from local HYPRE indices to local Dune indices.
Definition: HypreDataStructures.hpp:59
std::vector< HYPRE_BigInt > cols
Global column indices in CSR format (size: nnz)
Definition: HypreDataStructures.hpp:94
void destroyVector(HYPRE_IJVector vector)
Destroy Hypre vector.
Definition: HypreSetup.hpp:247
GpuVector< int > & getColumnIndices()
getColumnIndices returns the column indices used to represent the BSR structure.
Definition: GpuSparseMatrixWrapper.hpp:291
void setSolverParameters(HYPRE_Solver solver, const PropertyTree &prm, bool use_gpu_backend)
Set solver parameters from property tree.
Definition: HypreSetup.hpp:133
Compressed Sparse Row (CSR) sparsity pattern for HYPRE matrix assembly.
Definition: HypreDataStructures.hpp:86
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38
std::vector< HYPRE_Int > computeRowIndexes(const MatrixType &matrix, const std::vector< HYPRE_Int > &ncols, const std::vector< int > &local_dune_to_local_hypre, bool owner_first)
Compute row indexes for HYPRE_IJMatrixSetValues2.
Definition: HypreSetup.hpp:636
Parallel domain decomposition information for HYPRE-Dune interface.
Definition: HypreDataStructures.hpp:37
void destroySolver(HYPRE_Solver solver)
Destroy Hypre solver.
Definition: HypreSetup.hpp:219