Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / compressed_row_jacobian_writer.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 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
31 #include "ceres/compressed_row_jacobian_writer.h"
32
33 #include <iterator>
34 #include <utility>
35 #include <vector>
36
37 #include "ceres/casts.h"
38 #include "ceres/compressed_row_sparse_matrix.h"
39 #include "ceres/parameter_block.h"
40 #include "ceres/program.h"
41 #include "ceres/residual_block.h"
42 #include "ceres/scratch_evaluate_preparer.h"
43
44 namespace ceres {
45 namespace internal {
46
47 using std::make_pair;
48 using std::pair;
49 using std::vector;
50 using std::adjacent_find;
51
52 void CompressedRowJacobianWriter::PopulateJacobianRowAndColumnBlockVectors(
53     const Program* program, CompressedRowSparseMatrix* jacobian) {
54   const vector<ParameterBlock*>& parameter_blocks =
55       program->parameter_blocks();
56   vector<int>& col_blocks = *(jacobian->mutable_col_blocks());
57   col_blocks.resize(parameter_blocks.size());
58   for (int i = 0; i < parameter_blocks.size(); ++i) {
59     col_blocks[i] = parameter_blocks[i]->LocalSize();
60   }
61
62   const vector<ResidualBlock*>& residual_blocks =
63       program->residual_blocks();
64   vector<int>& row_blocks = *(jacobian->mutable_row_blocks());
65   row_blocks.resize(residual_blocks.size());
66   for (int i = 0; i < residual_blocks.size(); ++i) {
67     row_blocks[i] = residual_blocks[i]->NumResiduals();
68   }
69 }
70
71 void CompressedRowJacobianWriter::GetOrderedParameterBlocks(
72       const Program* program,
73       int residual_id,
74       vector<pair<int, int> >* evaluated_jacobian_blocks) {
75   const ResidualBlock* residual_block =
76       program->residual_blocks()[residual_id];
77   const int num_parameter_blocks = residual_block->NumParameterBlocks();
78
79   for (int j = 0; j < num_parameter_blocks; ++j) {
80     const ParameterBlock* parameter_block =
81         residual_block->parameter_blocks()[j];
82     if (!parameter_block->IsConstant()) {
83       evaluated_jacobian_blocks->push_back(
84           make_pair(parameter_block->index(), j));
85     }
86   }
87   sort(evaluated_jacobian_blocks->begin(), evaluated_jacobian_blocks->end());
88 }
89
90 SparseMatrix* CompressedRowJacobianWriter::CreateJacobian() const {
91   const vector<ResidualBlock*>& residual_blocks =
92       program_->residual_blocks();
93
94   int total_num_residuals = program_->NumResiduals();
95   int total_num_effective_parameters = program_->NumEffectiveParameters();
96
97   // Count the number of jacobian nonzeros.
98   int num_jacobian_nonzeros = 0;
99   for (int i = 0; i < residual_blocks.size(); ++i) {
100     ResidualBlock* residual_block = residual_blocks[i];
101     const int num_residuals = residual_block->NumResiduals();
102     const int num_parameter_blocks = residual_block->NumParameterBlocks();
103     for (int j = 0; j < num_parameter_blocks; ++j) {
104       ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
105       if (!parameter_block->IsConstant()) {
106         num_jacobian_nonzeros += num_residuals * parameter_block->LocalSize();
107       }
108     }
109   }
110
111   // Allocate storage for the jacobian with some extra space at the end.
112   // Allocate more space than needed to store the jacobian so that when the LM
113   // algorithm adds the diagonal, no reallocation is necessary. This reduces
114   // peak memory usage significantly.
115   CompressedRowSparseMatrix* jacobian =
116       new CompressedRowSparseMatrix(
117           total_num_residuals,
118           total_num_effective_parameters,
119           num_jacobian_nonzeros + total_num_effective_parameters);
120
121   // At this stage, the CompressedRowSparseMatrix is an invalid state. But this
122   // seems to be the only way to construct it without doing a memory copy.
123   int* rows = jacobian->mutable_rows();
124   int* cols = jacobian->mutable_cols();
125
126   int row_pos = 0;
127   rows[0] = 0;
128   for (int i = 0; i < residual_blocks.size(); ++i) {
129     const ResidualBlock* residual_block = residual_blocks[i];
130     const int num_parameter_blocks = residual_block->NumParameterBlocks();
131
132     // Count the number of derivatives for a row of this residual block and
133     // build a list of active parameter block indices.
134     int num_derivatives = 0;
135     vector<int> parameter_indices;
136     for (int j = 0; j < num_parameter_blocks; ++j) {
137       ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
138       if (!parameter_block->IsConstant()) {
139         parameter_indices.push_back(parameter_block->index());
140         num_derivatives += parameter_block->LocalSize();
141       }
142     }
143
144     // Sort the parameters by their position in the state vector.
145     sort(parameter_indices.begin(), parameter_indices.end());
146     if (adjacent_find(parameter_indices.begin(), parameter_indices.end()) !=
147         parameter_indices.end()) {
148       std::string parameter_block_description;
149       for (int j = 0; j < num_parameter_blocks; ++j) {
150         ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
151         parameter_block_description +=
152             parameter_block->ToString() + "\n";
153       }
154       LOG(FATAL) << "Ceres internal error: "
155                  << "Duplicate parameter blocks detected in a cost function. "
156                  << "This should never happen. Please report this to "
157                  << "the Ceres developers.\n"
158                  << "Residual Block: " << residual_block->ToString() << "\n"
159                  << "Parameter Blocks: " << parameter_block_description;
160     }
161
162     // Update the row indices.
163     const int num_residuals = residual_block->NumResiduals();
164     for (int j = 0; j < num_residuals; ++j) {
165       rows[row_pos + j + 1] = rows[row_pos + j] + num_derivatives;
166     }
167
168     // Iterate over parameter blocks in the order which they occur in the
169     // parameter vector. This code mirrors that in Write(), where jacobian
170     // values are updated.
171     int col_pos = 0;
172     for (int j = 0; j < parameter_indices.size(); ++j) {
173       ParameterBlock* parameter_block =
174           program_->parameter_blocks()[parameter_indices[j]];
175       const int parameter_block_size = parameter_block->LocalSize();
176
177       for (int r = 0; r < num_residuals; ++r) {
178         // This is the position in the values array of the jacobian where this
179         // row of the jacobian block should go.
180         const int column_block_begin = rows[row_pos + r] + col_pos;
181
182         for (int c = 0; c < parameter_block_size; ++c) {
183           cols[column_block_begin + c] = parameter_block->delta_offset() + c;
184         }
185       }
186       col_pos += parameter_block_size;
187     }
188     row_pos += num_residuals;
189   }
190   CHECK_EQ(num_jacobian_nonzeros, rows[total_num_residuals]);
191
192   PopulateJacobianRowAndColumnBlockVectors(program_, jacobian);
193
194   return jacobian;
195 }
196
197 void CompressedRowJacobianWriter::Write(int residual_id,
198                                         int residual_offset,
199                                         double **jacobians,
200                                         SparseMatrix* base_jacobian) {
201   CompressedRowSparseMatrix* jacobian =
202       down_cast<CompressedRowSparseMatrix*>(base_jacobian);
203
204   double* jacobian_values = jacobian->mutable_values();
205   const int* jacobian_rows = jacobian->rows();
206
207   const ResidualBlock* residual_block =
208       program_->residual_blocks()[residual_id];
209   const int num_residuals = residual_block->NumResiduals();
210
211   vector<pair<int, int> > evaluated_jacobian_blocks;
212   GetOrderedParameterBlocks(program_, residual_id, &evaluated_jacobian_blocks);
213
214   // Where in the current row does the jacobian for a parameter block begin.
215   int col_pos = 0;
216
217   // Iterate over the jacobian blocks in increasing order of their
218   // positions in the reduced parameter vector.
219   for (int i = 0; i < evaluated_jacobian_blocks.size(); ++i) {
220     const ParameterBlock* parameter_block =
221         program_->parameter_blocks()[evaluated_jacobian_blocks[i].first];
222     const int argument = evaluated_jacobian_blocks[i].second;
223     const int parameter_block_size = parameter_block->LocalSize();
224
225     // Copy one row of the jacobian block at a time.
226     for (int r = 0; r < num_residuals; ++r) {
227       // Position of the r^th row of the current jacobian block.
228       const double* block_row_begin =
229           jacobians[argument] + r * parameter_block_size;
230
231       // Position in the values array of the jacobian where this
232       // row of the jacobian block should go.
233       double* column_block_begin =
234           jacobian_values + jacobian_rows[residual_offset + r] + col_pos;
235
236       std::copy(block_row_begin,
237                 block_row_begin + parameter_block_size,
238                 column_block_begin);
239     }
240     col_pos += parameter_block_size;
241   }
242 }
243
244 }  // namespace internal
245 }  // namespace ceres