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