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