12 #include "../config/config.hpp"
14 #ifdef MFEM_USE_GINKGO
18 #include "../general/globals.hpp"
19 #include "../general/error.hpp"
28 namespace GinkgoWrappers
32 const std::string &exec_type,
int print_iter,
int max_num_iter,
33 double RTOLERANCE,
double ATOLERANCE)
34 : exec_type(exec_type),
35 print_lvl(print_iter),
36 max_iter(max_num_iter),
37 rel_tol(sqrt(RTOLERANCE)),
38 abs_tol(sqrt(ATOLERANCE))
40 if (exec_type ==
"reference")
42 executor = gko::ReferenceExecutor::create();
44 else if (exec_type ==
"omp")
46 executor = gko::OmpExecutor::create();
48 else if (exec_type ==
"cuda" && gko::CudaExecutor::get_num_devices() > 0)
50 executor = gko::CudaExecutor::create(0, gko::OmpExecutor::create());
55 " exec_type needs to be one of the three strings: \"reference\", \"cuda\" or \"omp\" "
58 using ResidualCriterionFactory = gko::stop::ResidualNormReduction<>;
64 gko::stop::Combined::build()
66 gko::stop::Iteration::build()
73 GinkgoIterativeSolverBase::initialize_ginkgo_log(gko::matrix::Dense<double>*
b)
78 executor, gko::log::Logger::criterion_check_completed_mask);
80 gko::lend(system_matrix),
b);
89 using val_array = gko::Array<double>;
90 using vec = gko::matrix::Dense<double>;
92 MFEM_VERIFY(system_matrix,
"System matrix not initialized");
93 MFEM_VERIFY(
executor,
"executor is not initialized");
94 MFEM_VERIFY(rhs.
Size() == solution.
Size(),
95 "Mismatching sizes for rhs and solution");
97 std::vector<double>
f(rhs.
Size());
101 gko::dim<2>(rhs.
Size(), 1),
102 val_array::view(
executor->get_master(), rhs.
Size(),
f.data()),
106 std::vector<double> u(solution.
Size());
109 gko::dim<2>(solution.
Size(), 1),
110 val_array::view(
executor->get_master(),
117 initialize_ginkgo_log(gko::lend(b));
127 auto solver =
solver_gen->generate(system_matrix);
134 solver->apply(gko::lend(b), gko::lend(x));
143 auto residual_norm_d =
144 gko::as<gko::matrix::Dense<double>>(residual_norm);
145 auto residual_norm_d_master =
146 gko::matrix::Dense<double>::create(
executor->get_master(),
148 residual_norm_d_master->copy_from(residual_norm_d);
156 auto b_norm = gko::matrix::Dense<double>::create(
executor->get_master(),
160 auto b_master = vec::create(
executor->get_master(),
161 gko::dim<2>(rhs.
Size(), 1),
162 val_array::view(
executor->get_master(),
166 b_master->compute_norm2(b_norm.get());
170 b->compute_norm2(b_norm.get());
173 MFEM_VERIFY(b_norm.get()->at(0, 0) != 0.0,
" rhs norm is zero");
178 auto fin_res_norm = std::pow(residual_norm_d_master->at(0,0) / b_norm->at(0,0),
196 mfem::out <<
"(B r_N, r_N) = " << fin_res_norm <<
'\n'
197 <<
"Number of iterations: " << num_iteration <<
'\n';
201 mfem::out <<
"Converged in " << num_iteration <<
202 " iterations with final residual norm "
203 << fin_res_norm <<
'\n';
210 auto x_master = vec::create(
executor->get_master(),
211 gko::dim<2>(solution.
Size(), 1),
216 x.reset(x_master.release());
219 std::copy(x->get_values(),
220 x->get_values() + solution.
Size(),
229 MFEM_VERIFY(matrix->
Height() == matrix->
Width(),
"System matrix is not square");
231 const int N = matrix->
Size();
232 using mtx = gko::matrix::Csr<double, int>;
233 std::shared_ptr<mtx> system_matrix_compute;
234 system_matrix_compute = mtx::create(
executor->get_master(),
237 double *mat_values = system_matrix_compute->get_values();
238 int *mat_row_ptrs = system_matrix_compute->get_row_ptrs();
239 int *mat_col_idxs = system_matrix_compute->get_col_idxs();
241 for (
int r=0; r< N; ++r)
245 mat_row_ptrs[r+1] = mat_row_ptrs[r] + matrix->
RowSize(r);
246 for (
int cj=0; cj < matrix->
RowSize(r); cj++ )
248 mat_values[mat_row_ptrs[r]+cj] = val[cj];
249 mat_col_idxs[mat_row_ptrs[r]+cj] = col[cj];
254 system_matrix->copy_from(system_matrix_compute.get());
263 apply(solution, rhs);
269 const std::string &exec_type,
278 using cg = gko::solver::Cg<double>;
284 const std::string &exec_type,
289 const gko::LinOpFactory* preconditioner
294 using cg = gko::solver::Cg<double>;
297 .with_preconditioner(preconditioner)
304 const std::string &exec_type,
313 using bicgstab = gko::solver::Bicgstab<double>;
320 const std::string &exec_type,
325 const gko::LinOpFactory* preconditioner
330 using bicgstab = gko::solver::Bicgstab<double>;
333 .with_preconditioner(preconditioner)
340 const std::string &exec_type,
349 using cgs = gko::solver::Cgs<double>;
355 const std::string &exec_type,
360 const gko::LinOpFactory* preconditioner
365 using cgs = gko::solver::Cgs<double>;
368 .with_preconditioner(preconditioner)
375 const std::string &exec_type,
384 using fcg = gko::solver::Fcg<double>;
390 const std::string &exec_type,
395 const gko::LinOpFactory* preconditioner
400 using fcg = gko::solver::Fcg<double>;
403 .with_preconditioner(preconditioner)
410 const std::string &exec_type,
419 using gmres = gko::solver::Gmres<double>;
427 const std::string &exec_type,
432 const gko::LinOpFactory* preconditioner
437 using gmres = gko::solver::Gmres<double>;
441 .with_preconditioner(preconditioner)
448 const std::string & exec_type,
457 using ir = gko::solver::Ir<double>;
463 const std::string &exec_type,
468 const gko::LinOpFactory* inner_solver
473 using ir = gko::solver::Ir<double>;
476 .with_solver(inner_solver)
485 #endif // MFEM_USE_GINKGO
CGSSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
int RowSize(const int i) const
Returns the number of elements in row i.
virtual int NumNonZeroElems() const
Returns the number of the nonzero elements in the matrix.
FCGSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
int * GetRowColumns(const int row)
Return a pointer to the column indices in a row.
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
std::shared_ptr< ResidualLogger<> > residual_logger
void solve(const SparseMatrix *matrix, Vector &solution, const Vector &rhs)
int Size() const
Returns the size of the vector.
GMRESSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
std::shared_ptr< gko::LinOpFactory > solver_gen
std::shared_ptr< gko::stop::Combined::Factory > combined_factory
double * GetRowEntries(const int row)
Return a pointer to the entries in a row.
double * GetData() const
Return a pointer to the beginning of the Vector data.
CGSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
std::shared_ptr< gko::stop::ResidualNormReduction<>::Factory > residual_criterion
void initialize(const SparseMatrix *matrix)
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
void apply(Vector &solution, const Vector &rhs)
int Size() const
For backward compatibility, define Size() to be synonym of Height().
BICGSTABSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
std::shared_ptr< gko::log::Convergence<> > convergence_logger
std::shared_ptr< gko::Executor > executor
IRSolver(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
GinkgoIterativeSolverBase(const std::string &exec_type, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
double f(const Vector &p)