123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #ifndef CERES_INTERNAL_CONJUGATE_GRADIENTS_SOLVER_H_
- #define CERES_INTERNAL_CONJUGATE_GRADIENTS_SOLVER_H_
- #include <cmath>
- #include <cstddef>
- #include <utility>
- #include "ceres/eigen_vector_ops.h"
- #include "ceres/internal/disable_warnings.h"
- #include "ceres/internal/eigen.h"
- #include "ceres/internal/export.h"
- #include "ceres/linear_operator.h"
- #include "ceres/linear_solver.h"
- #include "ceres/stringprintf.h"
- #include "ceres/types.h"
- #include "glog/logging.h"
- namespace ceres::internal {
- template <typename DenseVectorType>
- class ConjugateGradientsLinearOperator {
- public:
- ~ConjugateGradientsLinearOperator() = default;
- virtual void RightMultiplyAndAccumulate(const DenseVectorType& x,
- DenseVectorType& y) = 0;
- };
- class LinearOperatorAdapter : public ConjugateGradientsLinearOperator<Vector> {
- public:
- LinearOperatorAdapter(LinearOperator& linear_operator)
- : linear_operator_(linear_operator) {}
- void RightMultiplyAndAccumulate(const Vector& x, Vector& y) final {
- linear_operator_.RightMultiplyAndAccumulate(x, y);
- }
- private:
- LinearOperator& linear_operator_;
- };
- struct ConjugateGradientsSolverOptions {
- int min_num_iterations = 1;
- int max_num_iterations = 1;
- int residual_reset_period = 10;
- double r_tolerance = 0.0;
- double q_tolerance = 0.0;
- ContextImpl* context = nullptr;
- int num_threads = 1;
- };
- template <typename DenseVectorType>
- LinearSolver::Summary ConjugateGradientsSolver(
- const ConjugateGradientsSolverOptions options,
- ConjugateGradientsLinearOperator<DenseVectorType>& lhs,
- const DenseVectorType& rhs,
- ConjugateGradientsLinearOperator<DenseVectorType>& preconditioner,
- DenseVectorType* scratch[4],
- DenseVectorType& solution) {
- auto IsZeroOrInfinity = [](double x) {
- return ((x == 0.0) || std::isinf(x));
- };
- DenseVectorType& p = *scratch[0];
- DenseVectorType& r = *scratch[1];
- DenseVectorType& z = *scratch[2];
- DenseVectorType& tmp = *scratch[3];
- LinearSolver::Summary summary;
- summary.termination_type = LinearSolverTerminationType::NO_CONVERGENCE;
- summary.message = "Maximum number of iterations reached.";
- summary.num_iterations = 0;
- const double norm_rhs = Norm(rhs, options.context, options.num_threads);
- if (norm_rhs == 0.0) {
- SetZero(solution, options.context, options.num_threads);
- summary.termination_type = LinearSolverTerminationType::SUCCESS;
- summary.message = "Convergence. |b| = 0.";
- return summary;
- }
- const double tol_r = options.r_tolerance * norm_rhs;
- SetZero(tmp, options.context, options.num_threads);
- lhs.RightMultiplyAndAccumulate(solution, tmp);
-
- Axpby(1.0, rhs, -1.0, tmp, r, options.context, options.num_threads);
- double norm_r = Norm(r, options.context, options.num_threads);
- if (options.min_num_iterations == 0 && norm_r <= tol_r) {
- summary.termination_type = LinearSolverTerminationType::SUCCESS;
- summary.message =
- StringPrintf("Convergence. |r| = %e <= %e.", norm_r, tol_r);
- return summary;
- }
- double rho = 1.0;
-
-
- Axpby(1.0, rhs, 1.0, r, tmp, options.context, options.num_threads);
- double Q0 = -Dot(solution, tmp, options.context, options.num_threads);
- for (summary.num_iterations = 1;; ++summary.num_iterations) {
- SetZero(z, options.context, options.num_threads);
- preconditioner.RightMultiplyAndAccumulate(r, z);
- const double last_rho = rho;
-
- rho = Dot(r, z, options.context, options.num_threads);
- if (IsZeroOrInfinity(rho)) {
- summary.termination_type = LinearSolverTerminationType::FAILURE;
- summary.message = StringPrintf("Numerical failure. rho = r'z = %e.", rho);
- break;
- }
- if (summary.num_iterations == 1) {
- Copy(z, p, options.context, options.num_threads);
- } else {
- const double beta = rho / last_rho;
- if (IsZeroOrInfinity(beta)) {
- summary.termination_type = LinearSolverTerminationType::FAILURE;
- summary.message = StringPrintf(
- "Numerical failure. beta = rho_n / rho_{n-1} = %e, "
- "rho_n = %e, rho_{n-1} = %e",
- beta,
- rho,
- last_rho);
- break;
- }
-
- Axpby(1.0, z, beta, p, p, options.context, options.num_threads);
- }
- DenseVectorType& q = z;
- SetZero(q, options.context, options.num_threads);
- lhs.RightMultiplyAndAccumulate(p, q);
- const double pq = Dot(p, q, options.context, options.num_threads);
- if ((pq <= 0) || std::isinf(pq)) {
- summary.termination_type = LinearSolverTerminationType::NO_CONVERGENCE;
- summary.message = StringPrintf(
- "Matrix is indefinite, no more progress can be made. "
- "p'q = %e. |p| = %e, |q| = %e",
- pq,
- Norm(p, options.context, options.num_threads),
- Norm(q, options.context, options.num_threads));
- break;
- }
- const double alpha = rho / pq;
- if (std::isinf(alpha)) {
- summary.termination_type = LinearSolverTerminationType::FAILURE;
- summary.message = StringPrintf(
- "Numerical failure. alpha = rho / pq = %e, rho = %e, pq = %e.",
- alpha,
- rho,
- pq);
- break;
- }
-
- Axpby(1.0,
- solution,
- alpha,
- p,
- solution,
- options.context,
- options.num_threads);
-
-
-
-
-
-
-
- if (summary.num_iterations % options.residual_reset_period == 0) {
- SetZero(tmp, options.context, options.num_threads);
- lhs.RightMultiplyAndAccumulate(solution, tmp);
- Axpby(1.0, rhs, -1.0, tmp, r, options.context, options.num_threads);
-
- } else {
- Axpby(1.0, r, -alpha, q, r, options.context, options.num_threads);
-
- }
-
-
-
- Axpby(1.0, rhs, 1.0, r, tmp, options.context, options.num_threads);
- const double Q1 = -Dot(solution, tmp, options.context, options.num_threads);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- const double zeta = summary.num_iterations * (Q1 - Q0) / Q1;
- if (zeta < options.q_tolerance &&
- summary.num_iterations >= options.min_num_iterations) {
- summary.termination_type = LinearSolverTerminationType::SUCCESS;
- summary.message =
- StringPrintf("Iteration: %d Convergence: zeta = %e < %e. |r| = %e",
- summary.num_iterations,
- zeta,
- options.q_tolerance,
- Norm(r, options.context, options.num_threads));
- break;
- }
- Q0 = Q1;
-
- norm_r = Norm(r, options.context, options.num_threads);
- if (norm_r <= tol_r &&
- summary.num_iterations >= options.min_num_iterations) {
- summary.termination_type = LinearSolverTerminationType::SUCCESS;
- summary.message =
- StringPrintf("Iteration: %d Convergence. |r| = %e <= %e.",
- summary.num_iterations,
- norm_r,
- tol_r);
- break;
- }
- if (summary.num_iterations >= options.max_num_iterations) {
- break;
- }
- }
- return summary;
- }
- }
- #include "ceres/internal/reenable_warnings.h"
- #endif
|