Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / sparse_normal_cholesky_solver.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2017 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: sameeragarwal@google.com (Sameer Agarwal)
30
31 #include "ceres/sparse_normal_cholesky_solver.h"
32
33 #include <algorithm>
34 #include <cstring>
35 #include <ctime>
36
37 #include "ceres/block_sparse_matrix.h"
38 #include "ceres/inner_product_computer.h"
39 #include "ceres/internal/eigen.h"
40 #include "ceres/internal/scoped_ptr.h"
41 #include "ceres/linear_solver.h"
42 #include "ceres/sparse_cholesky.h"
43 #include "ceres/triplet_sparse_matrix.h"
44 #include "ceres/types.h"
45 #include "ceres/wall_time.h"
46
47 namespace ceres {
48 namespace internal {
49
50 SparseNormalCholeskySolver::SparseNormalCholeskySolver(
51     const LinearSolver::Options& options)
52     : options_(options) {
53   sparse_cholesky_.reset(
54       SparseCholesky::Create(options_.sparse_linear_algebra_library_type,
55                              options_.use_postordering ? AMD : NATURAL));
56 }
57
58 SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {}
59
60 LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
61     BlockSparseMatrix* A,
62     const double* b,
63     const LinearSolver::PerSolveOptions& per_solve_options,
64     double* x) {
65   EventLogger event_logger("SparseNormalCholeskySolver::Solve");
66   LinearSolver::Summary summary;
67   summary.num_iterations = 1;
68   summary.termination_type = LINEAR_SOLVER_SUCCESS;
69   summary.message = "Success.";
70
71   const int num_cols = A->num_cols();
72   VectorRef(x, num_cols).setZero();
73   A->LeftMultiply(b, x);
74   event_logger.AddEvent("Compute RHS");
75
76   if (per_solve_options.D != NULL) {
77     // Temporarily append a diagonal block to the A matrix, but undo
78     // it before returning the matrix to the user.
79     scoped_ptr<BlockSparseMatrix> regularizer;
80     regularizer.reset(BlockSparseMatrix::CreateDiagonalMatrix(
81         per_solve_options.D, A->block_structure()->cols));
82     event_logger.AddEvent("Diagonal");
83     A->AppendRows(*regularizer);
84     event_logger.AddEvent("Append");
85   }
86   event_logger.AddEvent("Append Rows");
87
88   if (inner_product_computer_.get() == NULL) {
89     inner_product_computer_.reset(
90         InnerProductComputer::Create(*A, sparse_cholesky_->StorageType()));
91
92     event_logger.AddEvent("InnerProductComputer::Create");
93   }
94
95   inner_product_computer_->Compute();
96   event_logger.AddEvent("InnerProductComputer::Compute");
97
98   // TODO(sameeragarwal):
99
100   if (per_solve_options.D != NULL) {
101     A->DeleteRowBlocks(A->block_structure()->cols.size());
102   }
103   summary.termination_type = sparse_cholesky_->FactorAndSolve(
104       inner_product_computer_->mutable_result(), x, x, &summary.message);
105   event_logger.AddEvent("Factor & Solve");
106   return summary;
107 }
108
109 }  // namespace internal
110 }  // namespace ceres