20#ifdef DEAL_II_WITH_GINKGO
32 template <
typename ValueType,
typename IndexType>
34 const std::string &exec_type)
35 : solver_control(solver_control)
36 , exec_type(exec_type)
40 executor = gko::ReferenceExecutor::create();
44 executor = gko::OmpExecutor::create();
46 else if (
exec_type ==
"cuda" && gko::CudaExecutor::get_num_devices() > 0)
48 executor = gko::CudaExecutor::create(0, gko::OmpExecutor::create());
55 " exec_type needs to be one of the three strings: \"reference\", \"cuda\" or \"omp\" "));
57 using ResidualCriterionFactory = gko::stop::ResidualNormReduction<>;
63 gko::stop::Combined::build()
65 gko::stop::Iteration::build()
73 template <
typename ValueType,
typename IndexType>
79 convergence_logger = gko::log::Convergence<>::create(
80 executor, gko::log::Logger::criterion_check_completed_mask);
85 template <
typename ValueType,
typename IndexType>
91 using val_array = gko::Array<ValueType>;
92 using vec = gko::matrix::Dense<ValueType>;
100 auto solver = solver_gen->generate(system_matrix);
103 std::vector<ValueType> f(rhs.
size());
106 vec::create(executor,
107 gko::dim<2>(rhs.
size(), 1),
108 val_array::view(executor->get_master(), rhs.
size(), f.data()),
112 std::vector<ValueType> u(solution.
size());
114 auto x = vec::create(executor,
115 gko::dim<2>(solution.
size(), 1),
116 val_array::view(executor->get_master(),
123 initialize_ginkgo_log();
128 combined_factory->add_logger(convergence_logger);
131 solver->apply(gko::lend(b), gko::lend(x));
140 auto residual_norm = convergence_logger->get_residual_norm();
141 auto residual_norm_d =
142 gko::as<gko::matrix::Dense<ValueType>>(residual_norm);
143 auto residual_norm_d_parent =
144 gko::matrix::Dense<ValueType>::create(executor->get_master(),
146 residual_norm_d_parent->copy_from(residual_norm_d);
149 auto num_iteration = convergence_logger->get_num_iterations();
154 auto b_norm = gko::matrix::Dense<ValueType>::create(executor->get_master(),
156 if (executor != executor->get_master())
158 auto b_master = vec::create(executor->get_master(),
159 gko::dim<2>(rhs.
size(), 1),
160 val_array::view(executor->get_master(),
164 b_master->compute_norm2(b_norm.get());
168 b->compute_norm2(b_norm.get());
177 solver_control.check(num_iteration,
178 residual_norm_d_parent->at(0, 0) / b_norm->at(0, 0));
184 solver_control.last_value()));
188 if (executor != executor->get_master())
190 auto x_master = vec::create(executor->get_master(),
191 gko::dim<2>(solution.
size(), 1),
192 val_array::view(executor,
196 x.reset(x_master.release());
199 std::copy(x->get_values(),
200 x->get_values() + solution.
size(),
206 template <
typename ValueType,
typename IndexType>
210 return solver_control;
215 template <
typename ValueType,
typename IndexType>
224 const size_type N = matrix.m();
226 using mtx = gko::matrix::Csr<ValueType, IndexType>;
227 std::shared_ptr<mtx> system_matrix_compute;
228 system_matrix_compute = mtx::create(executor->get_master(),
230 matrix.n_nonzero_elements());
231 ValueType *mat_values = system_matrix_compute->get_values();
232 IndexType *mat_row_ptrs = system_matrix_compute->get_row_ptrs();
233 IndexType *mat_col_idxs = system_matrix_compute->get_col_idxs();
244 for (size_type row = 1; row <= N; ++row)
246 mat_row_ptrs[row - 1] + matrix.get_row_length(row - 1);
255 std::vector<IndexType> row_pointers(N + 1);
256 std::copy(system_matrix_compute->get_row_ptrs(),
257 system_matrix_compute->get_row_ptrs() + N + 1,
258 row_pointers.begin());
262 for (size_type row = 0; row < N; ++row)
266 p != matrix.end(row);
270 mat_col_idxs[row_pointers[row]] = p->column();
271 mat_values[row_pointers[row]] = p->value();
279 for (size_type i = 0; i < N - 1; ++i)
283 mtx::create(executor, gko::dim<2>(N), matrix.n_nonzero_elements());
284 system_matrix->copy_from(system_matrix_compute.get());
289 template <
typename ValueType,
typename IndexType>
296 apply(solution, rhs);
302 template <
typename ValueType,
typename IndexType>
304 const std::string &exec_type,
306 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
307 , additional_data(data)
309 using cg = gko::solver::Cg<ValueType>;
316 template <
typename ValueType,
typename IndexType>
319 const std::string &exec_type,
320 const std::shared_ptr<gko::LinOpFactory> &preconditioner,
322 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
323 , additional_data(data)
325 using cg = gko::solver::Cg<ValueType>;
328 .with_preconditioner(preconditioner)
335 template <
typename ValueType,
typename IndexType>
338 const std::string &exec_type,
340 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
341 , additional_data(data)
343 using bicgstab = gko::solver::Bicgstab<ValueType>;
351 template <
typename ValueType,
typename IndexType>
354 const std::string &exec_type,
355 const std::shared_ptr<gko::LinOpFactory> &preconditioner,
357 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
358 , additional_data(data)
360 using bicgstab = gko::solver::Bicgstab<ValueType>;
363 .with_preconditioner(preconditioner)
370 template <
typename ValueType,
typename IndexType>
372 const std::string &exec_type,
374 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
375 , additional_data(data)
377 using cgs = gko::solver::Cgs<ValueType>;
384 template <
typename ValueType,
typename IndexType>
387 const std::string &exec_type,
388 const std::shared_ptr<gko::LinOpFactory> &preconditioner,
390 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
391 , additional_data(data)
393 using cgs = gko::solver::Cgs<ValueType>;
396 .with_preconditioner(preconditioner)
403 template <
typename ValueType,
typename IndexType>
405 const std::string &exec_type,
407 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
408 , additional_data(data)
410 using fcg = gko::solver::Fcg<ValueType>;
417 template <
typename ValueType,
typename IndexType>
420 const std::string &exec_type,
421 const std::shared_ptr<gko::LinOpFactory> &preconditioner,
423 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
424 , additional_data(data)
426 using fcg = gko::solver::Fcg<ValueType>;
429 .with_preconditioner(preconditioner)
436 template <
typename ValueType,
typename IndexType>
438 const unsigned int restart_parameter)
439 : restart_parameter(restart_parameter)
444 template <
typename ValueType,
typename IndexType>
451 using gmres = gko::solver::Gmres<ValueType>;
460 template <
typename ValueType,
typename IndexType>
463 const std::string &exec_type,
464 const std::shared_ptr<gko::LinOpFactory> &preconditioner,
466 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
467 , additional_data(data)
469 using gmres = gko::solver::Gmres<ValueType>;
473 .with_preconditioner(preconditioner)
480 template <
typename ValueType,
typename IndexType>
482 const std::string &exec_type,
484 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
485 , additional_data(data)
487 using ir = gko::solver::Ir<ValueType>;
494 template <
typename ValueType,
typename IndexType>
497 const std::string &exec_type,
498 const std::shared_ptr<gko::LinOpFactory> &inner_solver,
500 :
SolverBase<ValueType, IndexType>(solver_control, exec_type)
501 , additional_data(data)
503 using ir = gko::solver::Ir<ValueType>;
506 .with_solver(inner_solver)
513# define DEALII_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
514 template _macro(float, int32_t); \
515 template _macro(double, int32_t); \
516 template _macro(float, int64_t); \
517 template _macro(double, int64_t);
519# define DECLARE_SOLVER_BASE(ValueType, IndexType) \
520 class SolverBase<ValueType, IndexType>
522# undef DECLARE_SOLVER_BASE
524# define DECLARE_SOLVER_CG(ValueType, IndexType) \
525 class SolverCG<ValueType, IndexType>
527# undef DECLARE_SOLVER_CG
529# define DECLARE_SOLVER_Bicgstab(ValueType, IndexType) \
530 class SolverBicgstab<ValueType, IndexType>
532# undef DECLARE_SOLVER_Bicgstab
534# define DECLARE_SOLVER_CGS(ValueType, IndexType) \
535 class SolverCGS<ValueType, IndexType>
537# undef DECLARE_SOLVER_CGS
539# define DECLARE_SOLVER_FCG(ValueType, IndexType) \
540 class SolverFCG<ValueType, IndexType>
542# undef DECLARE_SOLVER_FCG
544# define DECLARE_SOLVER_GMRES(ValueType, IndexType) \
545 class SolverGMRES<ValueType, IndexType>
547# undef DECLARE_SOLVER_GMRES
549# define DECLARE_SOLVER_IR(ValueType, IndexType) \
550 class SolverIR<ValueType, IndexType>
552# undef DECLARE_SOLVER_IR
SolverControl & control() const
const std::string exec_type
SolverControl & solver_control
void initialize_ginkgo_log()
void initialize(const SparseMatrix< ValueType > &matrix)
SolverBase(SolverControl &solver_control, const std::string &exec_type)
void apply(Vector< ValueType > &solution, const Vector< ValueType > &rhs)
std::shared_ptr< gko::Executor > executor
std::shared_ptr< gko::LinOpFactory > solver_gen
void solve(const SparseMatrix< ValueType > &matrix, Vector< ValueType > &solution, const Vector< ValueType > &rhs)
std::shared_ptr< gko::stop::ResidualNormReduction<>::Factory > residual_criterion
std::shared_ptr< gko::stop::Combined::Factory > combined_factory
SolverBicgstab(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
SolverCGS(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
SolverCG(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
SolverFCG(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
const AdditionalData additional_data
SolverGMRES(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
SolverIR(SolverControl &solver_control, const std::string &exec_type, const AdditionalData &data=AdditionalData())
unsigned int max_steps() const
@ success
Stop iteration, goal reached.
virtual size_type size() const override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define DECLARE_SOLVER_Bicgstab(ValueType, IndexType)
#define DECLARE_SOLVER_FCG(ValueType, IndexType)
#define DECLARE_SOLVER_CG(ValueType, IndexType)
#define DECLARE_SOLVER_BASE(ValueType, IndexType)
#define DECLARE_SOLVER_IR(ValueType, IndexType)
#define DEALII_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro)
#define DECLARE_SOLVER_GMRES(ValueType, IndexType)
#define DECLARE_SOLVER_CGS(ValueType, IndexType)
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
unsigned int global_dof_index
AdditionalData(const unsigned int restart_parameter=30)
unsigned int restart_parameter