Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 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 | // The ProgramEvaluator runs the cost functions contained in each residual block |
| 32 | // and stores the result into a jacobian. The particular type of jacobian is |
| 33 | // abstracted out using two template parameters: |
| 34 | // |
| 35 | // - An "EvaluatePreparer" that is responsible for creating the array with |
| 36 | // pointers to the jacobian blocks where the cost function evaluates to. |
| 37 | // - A "JacobianWriter" that is responsible for storing the resulting |
| 38 | // jacobian blocks in the passed sparse matrix. |
| 39 | // |
Keir Mierle | cc38774 | 2012-05-03 01:27:50 -0700 | [diff] [blame] | 40 | // This abstraction affords an efficient evaluator implementation while still |
| 41 | // supporting writing to multiple sparse matrix formats. For example, when the |
| 42 | // ProgramEvaluator is parameterized for writing to block sparse matrices, the |
| 43 | // residual jacobians are written directly into their final position in the |
| 44 | // block sparse matrix by the user's CostFunction; there is no copying. |
| 45 | // |
| 46 | // The evaluation is threaded with OpenMP. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 47 | // |
| 48 | // The EvaluatePreparer and JacobianWriter interfaces are as follows: |
| 49 | // |
| 50 | // class EvaluatePreparer { |
| 51 | // // Prepare the jacobians array for use as the destination of a call to |
| 52 | // // a cost function's evaluate method. |
| 53 | // void Prepare(const ResidualBlock* residual_block, |
| 54 | // int residual_block_index, |
| 55 | // SparseMatrix* jacobian, |
| 56 | // double** jacobians); |
| 57 | // } |
| 58 | // |
| 59 | // class JacobianWriter { |
| 60 | // // Create a jacobian that this writer can write. Same as |
| 61 | // // Evaluator::CreateJacobian. |
| 62 | // SparseMatrix* CreateJacobian() const; |
| 63 | // |
| 64 | // // Create num_threads evaluate preparers. Caller owns result which must |
| 65 | // // be freed with delete[]. Resulting preparers are valid while *this is. |
| 66 | // EvaluatePreparer* CreateEvaluatePreparers(int num_threads); |
| 67 | // |
| 68 | // // Write the block jacobians from a residual block evaluation to the |
| 69 | // // larger sparse jacobian. |
| 70 | // void Write(int residual_id, |
| 71 | // int residual_offset, |
| 72 | // double** jacobians, |
| 73 | // SparseMatrix* jacobian); |
| 74 | // } |
| 75 | // |
| 76 | // Note: The ProgramEvaluator is not thread safe, since internally it maintains |
| 77 | // some per-thread scratch space. |
| 78 | |
| 79 | #ifndef CERES_INTERNAL_PROGRAM_EVALUATOR_H_ |
| 80 | #define CERES_INTERNAL_PROGRAM_EVALUATOR_H_ |
| 81 | |
| 82 | #ifdef CERES_USE_OPENMP |
| 83 | #include <omp.h> |
| 84 | #endif |
| 85 | |
| 86 | #include "ceres/parameter_block.h" |
| 87 | #include "ceres/program.h" |
| 88 | #include "ceres/residual_block.h" |
| 89 | #include "ceres/internal/eigen.h" |
| 90 | #include "ceres/internal/scoped_ptr.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 91 | #include "ceres/execution_summary.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 92 | |
| 93 | namespace ceres { |
| 94 | namespace internal { |
| 95 | |
| 96 | template<typename EvaluatePreparer, typename JacobianWriter> |
| 97 | class ProgramEvaluator : public Evaluator { |
| 98 | public: |
| 99 | ProgramEvaluator(const Evaluator::Options &options, Program* program) |
| 100 | : options_(options), |
| 101 | program_(program), |
| 102 | jacobian_writer_(options, program), |
| 103 | evaluate_preparers_( |
| 104 | jacobian_writer_.CreateEvaluatePreparers(options.num_threads)) { |
| 105 | #ifndef CERES_USE_OPENMP |
| 106 | CHECK_EQ(1, options_.num_threads) |
| 107 | << "OpenMP support is not compiled into this binary; " |
| 108 | << "only options.num_threads=1 is supported."; |
| 109 | #endif |
| 110 | |
| 111 | BuildResidualLayout(*program, &residual_layout_); |
| 112 | evaluate_scratch_.reset(CreateEvaluatorScratch(*program, |
| 113 | options.num_threads)); |
| 114 | } |
| 115 | |
| 116 | // Implementation of Evaluator interface. |
| 117 | SparseMatrix* CreateJacobian() const { |
| 118 | return jacobian_writer_.CreateJacobian(); |
| 119 | } |
| 120 | |
| 121 | bool Evaluate(const double* state, |
| 122 | double* cost, |
| 123 | double* residuals, |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 124 | double* gradient, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 125 | SparseMatrix* jacobian) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 126 | ScopedExecutionTimer total_timer("Evaluator::Total", &execution_summary_); |
| 127 | ScopedExecutionTimer call_type_timer(gradient == NULL && jacobian == NULL |
| 128 | ? "Evaluator::Residual" |
| 129 | : "Evaluator::Jacobian", |
| 130 | &execution_summary_); |
| 131 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 132 | // The parameters are stateful, so set the state before evaluating. |
| 133 | if (!program_->StateVectorToParameterBlocks(state)) { |
| 134 | return false; |
| 135 | } |
| 136 | |
Sameer Agarwal | 319ef46 | 2012-05-22 20:44:52 -0700 | [diff] [blame] | 137 | if (residuals != NULL) { |
| 138 | VectorRef(residuals, program_->NumResiduals()).setZero(); |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 139 | } |
Sameer Agarwal | 319ef46 | 2012-05-22 20:44:52 -0700 | [diff] [blame] | 140 | |
| 141 | if (jacobian != NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 142 | jacobian->SetZero(); |
| 143 | } |
| 144 | |
| 145 | // Each thread gets it's own cost and evaluate scratch space. |
| 146 | for (int i = 0; i < options_.num_threads; ++i) { |
| 147 | evaluate_scratch_[i].cost = 0.0; |
Sameer Agarwal | 31432ae | 2012-11-25 17:01:44 -0800 | [diff] [blame] | 148 | if (gradient != NULL) { |
| 149 | VectorRef(evaluate_scratch_[i].gradient.get(), |
| 150 | program_->NumEffectiveParameters()).setZero(); |
| 151 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // This bool is used to disable the loop if an error is encountered |
| 155 | // without breaking out of it. The remaining loop iterations are still run, |
| 156 | // but with an empty body, and so will finish quickly. |
| 157 | bool abort = false; |
| 158 | int num_residual_blocks = program_->NumResidualBlocks(); |
| 159 | #pragma omp parallel for num_threads(options_.num_threads) |
| 160 | for (int i = 0; i < num_residual_blocks; ++i) { |
| 161 | // Disable the loop instead of breaking, as required by OpenMP. |
| 162 | #pragma omp flush(abort) |
| 163 | if (abort) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | #ifdef CERES_USE_OPENMP |
| 168 | int thread_id = omp_get_thread_num(); |
| 169 | #else |
| 170 | int thread_id = 0; |
| 171 | #endif |
| 172 | EvaluatePreparer* preparer = &evaluate_preparers_[thread_id]; |
| 173 | EvaluateScratch* scratch = &evaluate_scratch_[thread_id]; |
| 174 | |
| 175 | // Prepare block residuals if requested. |
| 176 | const ResidualBlock* residual_block = program_->residual_blocks()[i]; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 177 | double* block_residuals = NULL; |
| 178 | if (residuals != NULL) { |
| 179 | block_residuals = residuals + residual_layout_[i]; |
| 180 | } else if (gradient != NULL) { |
| 181 | block_residuals = scratch->residual_block_residuals.get(); |
| 182 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 183 | |
| 184 | // Prepare block jacobians if requested. |
| 185 | double** block_jacobians = NULL; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 186 | if (jacobian != NULL || gradient != NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 187 | preparer->Prepare(residual_block, |
| 188 | i, |
| 189 | jacobian, |
| 190 | scratch->jacobian_block_ptrs.get()); |
| 191 | block_jacobians = scratch->jacobian_block_ptrs.get(); |
| 192 | } |
| 193 | |
| 194 | // Evaluate the cost, residuals, and jacobians. |
| 195 | double block_cost; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 196 | if (!residual_block->Evaluate( |
| 197 | &block_cost, |
| 198 | block_residuals, |
| 199 | block_jacobians, |
| 200 | scratch->residual_block_evaluate_scratch.get())) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 201 | abort = true; |
| 202 | // This ensures that the OpenMP threads have a consistent view of 'abort'. Do |
| 203 | // the flush inside the failure case so that there is usually only one |
| 204 | // synchronization point per loop iteration instead of two. |
| 205 | #pragma omp flush(abort) |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | scratch->cost += block_cost; |
| 210 | |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 211 | // Store the jacobians, if they were requested. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 212 | if (jacobian != NULL) { |
| 213 | jacobian_writer_.Write(i, |
| 214 | residual_layout_[i], |
| 215 | block_jacobians, |
| 216 | jacobian); |
| 217 | } |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 218 | |
| 219 | // Compute and store the gradient, if it was requested. |
| 220 | if (gradient != NULL) { |
| 221 | int num_residuals = residual_block->NumResiduals(); |
| 222 | int num_parameter_blocks = residual_block->NumParameterBlocks(); |
| 223 | for (int j = 0; j < num_parameter_blocks; ++j) { |
| 224 | const ParameterBlock* parameter_block = |
| 225 | residual_block->parameter_blocks()[j]; |
| 226 | if (parameter_block->IsConstant()) { |
| 227 | continue; |
| 228 | } |
| 229 | MatrixRef block_jacobian(block_jacobians[j], |
| 230 | num_residuals, |
| 231 | parameter_block->LocalSize()); |
| 232 | VectorRef block_gradient(scratch->gradient.get() + |
| 233 | parameter_block->delta_offset(), |
| 234 | parameter_block->LocalSize()); |
| 235 | VectorRef block_residual(block_residuals, num_residuals); |
| 236 | block_gradient += block_residual.transpose() * block_jacobian; |
| 237 | } |
| 238 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if (!abort) { |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 242 | // Sum the cost and gradient (if requested) from each thread. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 243 | (*cost) = 0.0; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 244 | int num_parameters = program_->NumEffectiveParameters(); |
| 245 | if (gradient != NULL) { |
| 246 | VectorRef(gradient, num_parameters).setZero(); |
| 247 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 248 | for (int i = 0; i < options_.num_threads; ++i) { |
| 249 | (*cost) += evaluate_scratch_[i].cost; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 250 | if (gradient != NULL) { |
| 251 | VectorRef(gradient, num_parameters) += |
| 252 | VectorRef(evaluate_scratch_[i].gradient.get(), num_parameters); |
| 253 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | return !abort; |
| 257 | } |
| 258 | |
| 259 | bool Plus(const double* state, |
| 260 | const double* delta, |
| 261 | double* state_plus_delta) const { |
| 262 | return program_->Plus(state, delta, state_plus_delta); |
| 263 | } |
| 264 | |
| 265 | int NumParameters() const { |
| 266 | return program_->NumParameters(); |
| 267 | } |
| 268 | int NumEffectiveParameters() const { |
| 269 | return program_->NumEffectiveParameters(); |
| 270 | } |
| 271 | |
| 272 | int NumResiduals() const { |
| 273 | return program_->NumResiduals(); |
| 274 | } |
| 275 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 276 | virtual map<string, int> CallStatistics() const { |
| 277 | return execution_summary_.calls(); |
| 278 | } |
| 279 | |
| 280 | virtual map<string, double> TimeStatistics() const { |
| 281 | return execution_summary_.times(); |
| 282 | } |
| 283 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 284 | private: |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 285 | // Per-thread scratch space needed to evaluate and store each residual block. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 286 | struct EvaluateScratch { |
| 287 | void Init(int max_parameters_per_residual_block, |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 288 | int max_scratch_doubles_needed_for_evaluate, |
| 289 | int max_residuals_per_residual_block, |
| 290 | int num_parameters) { |
| 291 | residual_block_evaluate_scratch.reset( |
| 292 | new double[max_scratch_doubles_needed_for_evaluate]); |
| 293 | gradient.reset(new double[num_parameters]); |
| 294 | VectorRef(gradient.get(), num_parameters).setZero(); |
| 295 | residual_block_residuals.reset( |
| 296 | new double[max_residuals_per_residual_block]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 297 | jacobian_block_ptrs.reset( |
| 298 | new double*[max_parameters_per_residual_block]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | double cost; |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 302 | scoped_array<double> residual_block_evaluate_scratch; |
| 303 | // The gradient in the local parameterization. |
| 304 | scoped_array<double> gradient; |
| 305 | // Enough space to store the residual for the largest residual block. |
| 306 | scoped_array<double> residual_block_residuals; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 307 | scoped_array<double*> jacobian_block_ptrs; |
| 308 | }; |
| 309 | |
| 310 | static void BuildResidualLayout(const Program& program, |
| 311 | vector<int>* residual_layout) { |
| 312 | const vector<ResidualBlock*>& residual_blocks = program.residual_blocks(); |
| 313 | residual_layout->resize(program.NumResidualBlocks()); |
| 314 | int residual_pos = 0; |
| 315 | for (int i = 0; i < residual_blocks.size(); ++i) { |
| 316 | const int num_residuals = residual_blocks[i]->NumResiduals(); |
| 317 | (*residual_layout)[i] = residual_pos; |
| 318 | residual_pos += num_residuals; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Create scratch space for each thread evaluating the program. |
| 323 | static EvaluateScratch* CreateEvaluatorScratch(const Program& program, |
| 324 | int num_threads) { |
| 325 | int max_parameters_per_residual_block = |
| 326 | program.MaxParametersPerResidualBlock(); |
| 327 | int max_scratch_doubles_needed_for_evaluate = |
| 328 | program.MaxScratchDoublesNeededForEvaluate(); |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 329 | int max_residuals_per_residual_block = |
| 330 | program.MaxResidualsPerResidualBlock(); |
| 331 | int num_parameters = program.NumEffectiveParameters(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 332 | |
| 333 | EvaluateScratch* evaluate_scratch = new EvaluateScratch[num_threads]; |
| 334 | for (int i = 0; i < num_threads; i++) { |
| 335 | evaluate_scratch[i].Init(max_parameters_per_residual_block, |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 336 | max_scratch_doubles_needed_for_evaluate, |
| 337 | max_residuals_per_residual_block, |
| 338 | num_parameters); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 339 | } |
| 340 | return evaluate_scratch; |
| 341 | } |
| 342 | |
| 343 | Evaluator::Options options_; |
| 344 | Program* program_; |
| 345 | JacobianWriter jacobian_writer_; |
| 346 | scoped_array<EvaluatePreparer> evaluate_preparers_; |
| 347 | scoped_array<EvaluateScratch> evaluate_scratch_; |
| 348 | vector<int> residual_layout_; |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 349 | ::ceres::internal::ExecutionSummary execution_summary_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | } // namespace internal |
| 353 | } // namespace ceres |
| 354 | |
| 355 | #endif // CERES_INTERNAL_PROGRAM_EVALUATOR_H_ |