cgnr_solver.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. #ifndef CERES_INTERNAL_CGNR_SOLVER_H_
  31. #define CERES_INTERNAL_CGNR_SOLVER_H_
  32. #include <memory>
  33. #include "ceres/conjugate_gradients_solver.h"
  34. #include "ceres/cuda_sparse_matrix.h"
  35. #include "ceres/cuda_vector.h"
  36. #include "ceres/internal/export.h"
  37. #include "ceres/linear_solver.h"
  38. namespace ceres::internal {
  39. class Preconditioner;
  40. class BlockJacobiPreconditioner;
  41. // A conjugate gradients on the normal equations solver. This directly solves
  42. // for the solution to
  43. //
  44. // (A^T A + D^T D)x = A^T b
  45. //
  46. // as required for solving for x in the least squares sense. Currently only
  47. // block diagonal preconditioning is supported.
  48. class CERES_NO_EXPORT CgnrSolver final : public BlockSparseMatrixSolver {
  49. public:
  50. explicit CgnrSolver(LinearSolver::Options options);
  51. CgnrSolver(const CgnrSolver&) = delete;
  52. void operator=(const CgnrSolver&) = delete;
  53. ~CgnrSolver() override;
  54. Summary SolveImpl(BlockSparseMatrix* A,
  55. const double* b,
  56. const LinearSolver::PerSolveOptions& per_solve_options,
  57. double* x) final;
  58. private:
  59. const LinearSolver::Options options_;
  60. std::unique_ptr<Preconditioner> preconditioner_;
  61. Vector cg_solution_;
  62. Vector* scratch_[4] = {nullptr, nullptr, nullptr, nullptr};
  63. };
  64. #ifndef CERES_NO_CUDA
  65. class CudaPreconditioner : public ConjugateGradientsLinearOperator<CudaVector> {
  66. public:
  67. virtual void Update(const CompressedRowSparseMatrix& A, const double* D) = 0;
  68. virtual ~CudaPreconditioner() = default;
  69. };
  70. // A Cuda-accelerated version of CgnrSolver.
  71. // This solver assumes that the sparsity structure of A remains constant for its
  72. // lifetime.
  73. class CERES_NO_EXPORT CudaCgnrSolver final
  74. : public CompressedRowSparseMatrixSolver {
  75. public:
  76. explicit CudaCgnrSolver(LinearSolver::Options options);
  77. static std::unique_ptr<CudaCgnrSolver> Create(LinearSolver::Options options,
  78. std::string* error);
  79. ~CudaCgnrSolver() override;
  80. Summary SolveImpl(CompressedRowSparseMatrix* A,
  81. const double* b,
  82. const LinearSolver::PerSolveOptions& per_solve_options,
  83. double* x) final;
  84. private:
  85. void CpuToGpuTransfer(const CompressedRowSparseMatrix& A,
  86. const double* b,
  87. const double* D);
  88. LinearSolver::Options options_;
  89. std::unique_ptr<CudaSparseMatrix> A_;
  90. std::unique_ptr<CudaVector> b_;
  91. std::unique_ptr<CudaVector> x_;
  92. std::unique_ptr<CudaVector> Atb_;
  93. std::unique_ptr<CudaVector> Ax_;
  94. std::unique_ptr<CudaVector> D_;
  95. std::unique_ptr<CudaPreconditioner> preconditioner_;
  96. CudaVector* scratch_[4] = {nullptr, nullptr, nullptr, nullptr};
  97. };
  98. #endif // CERES_NO_CUDA
  99. } // namespace ceres::internal
  100. #endif // CERES_INTERNAL_CGNR_SOLVER_H_