|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The ProgramEvaluator runs the cost functions contained in each residual block
|
|
Packit |
ea1746 |
// and stores the result into a jacobian. The particular type of jacobian is
|
|
Packit |
ea1746 |
// abstracted out using two template parameters:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// - An "EvaluatePreparer" that is responsible for creating the array with
|
|
Packit |
ea1746 |
// pointers to the jacobian blocks where the cost function evaluates to.
|
|
Packit |
ea1746 |
// - A "JacobianWriter" that is responsible for storing the resulting
|
|
Packit |
ea1746 |
// jacobian blocks in the passed sparse matrix.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// This abstraction affords an efficient evaluator implementation while still
|
|
Packit |
ea1746 |
// supporting writing to multiple sparse matrix formats. For example, when the
|
|
Packit |
ea1746 |
// ProgramEvaluator is parameterized for writing to block sparse matrices, the
|
|
Packit |
ea1746 |
// residual jacobians are written directly into their final position in the
|
|
Packit |
ea1746 |
// block sparse matrix by the user's CostFunction; there is no copying.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The evaluation is threaded with OpenMP.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The EvaluatePreparer and JacobianWriter interfaces are as follows:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// class EvaluatePreparer {
|
|
Packit |
ea1746 |
// // Prepare the jacobians array for use as the destination of a call to
|
|
Packit |
ea1746 |
// // a cost function's evaluate method.
|
|
Packit |
ea1746 |
// void Prepare(const ResidualBlock* residual_block,
|
|
Packit |
ea1746 |
// int residual_block_index,
|
|
Packit |
ea1746 |
// SparseMatrix* jacobian,
|
|
Packit |
ea1746 |
// double** jacobians);
|
|
Packit |
ea1746 |
// }
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// class JacobianWriter {
|
|
Packit |
ea1746 |
// // Create a jacobian that this writer can write. Same as
|
|
Packit |
ea1746 |
// // Evaluator::CreateJacobian.
|
|
Packit |
ea1746 |
// SparseMatrix* CreateJacobian() const;
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// // Create num_threads evaluate preparers. Caller owns result which must
|
|
Packit |
ea1746 |
// // be freed with delete[]. Resulting preparers are valid while *this is.
|
|
Packit |
ea1746 |
// EvaluatePreparer* CreateEvaluatePreparers(int num_threads);
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// // Write the block jacobians from a residual block evaluation to the
|
|
Packit |
ea1746 |
// // larger sparse jacobian.
|
|
Packit |
ea1746 |
// void Write(int residual_id,
|
|
Packit |
ea1746 |
// int residual_offset,
|
|
Packit |
ea1746 |
// double** jacobians,
|
|
Packit |
ea1746 |
// SparseMatrix* jacobian);
|
|
Packit |
ea1746 |
// }
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Note: The ProgramEvaluator is not thread safe, since internally it maintains
|
|
Packit |
ea1746 |
// some per-thread scratch space.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_PROGRAM_EVALUATOR_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_PROGRAM_EVALUATOR_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This include must come before any #ifndef check on Ceres compile options.
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifdef CERES_USE_OPENMP
|
|
Packit |
ea1746 |
#include <omp.h>
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <map>
|
|
Packit |
ea1746 |
#include <string>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
#include "ceres/execution_summary.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/parameter_block.h"
|
|
Packit |
ea1746 |
#include "ceres/program.h"
|
|
Packit |
ea1746 |
#include "ceres/residual_block.h"
|
|
Packit |
ea1746 |
#include "ceres/small_blas.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
struct NullJacobianFinalizer {
|
|
Packit |
ea1746 |
void operator()(SparseMatrix* jacobian, int num_parameters) {}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
template
|
|
Packit |
ea1746 |
typename JacobianWriter,
|
|
Packit |
ea1746 |
typename JacobianFinalizer = NullJacobianFinalizer>
|
|
Packit |
ea1746 |
class ProgramEvaluator : public Evaluator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
ProgramEvaluator(const Evaluator::Options &options, Program* program)
|
|
Packit |
ea1746 |
: options_(options),
|
|
Packit |
ea1746 |
program_(program),
|
|
Packit |
ea1746 |
jacobian_writer_(options, program),
|
|
Packit |
ea1746 |
evaluate_preparers_(
|
|
Packit |
ea1746 |
jacobian_writer_.CreateEvaluatePreparers(options.num_threads)) {
|
|
Packit |
ea1746 |
#ifndef CERES_USE_OPENMP
|
|
Packit |
ea1746 |
if (options_.num_threads > 1) {
|
|
Packit |
ea1746 |
LOG(WARNING)
|
|
Packit |
ea1746 |
<< "OpenMP support is not compiled into this binary; "
|
|
Packit |
ea1746 |
<< "only options.num_threads = 1 is supported. Switching "
|
|
Packit |
ea1746 |
<< "to single threaded mode.";
|
|
Packit |
ea1746 |
options_.num_threads = 1;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
BuildResidualLayout(*program, &residual_layout_);
|
|
Packit |
ea1746 |
evaluate_scratch_.reset(CreateEvaluatorScratch(*program,
|
|
Packit |
ea1746 |
options.num_threads));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Implementation of Evaluator interface.
|
|
Packit |
ea1746 |
SparseMatrix* CreateJacobian() const {
|
|
Packit |
ea1746 |
return jacobian_writer_.CreateJacobian();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool Evaluate(const Evaluator::EvaluateOptions& evaluate_options,
|
|
Packit |
ea1746 |
const double* state,
|
|
Packit |
ea1746 |
double* cost,
|
|
Packit |
ea1746 |
double* residuals,
|
|
Packit |
ea1746 |
double* gradient,
|
|
Packit |
ea1746 |
SparseMatrix* jacobian) {
|
|
Packit |
ea1746 |
ScopedExecutionTimer total_timer("Evaluator::Total", &execution_summary_);
|
|
Packit |
ea1746 |
ScopedExecutionTimer call_type_timer(gradient == NULL && jacobian == NULL
|
|
Packit |
ea1746 |
? "Evaluator::Residual"
|
|
Packit |
ea1746 |
: "Evaluator::Jacobian",
|
|
Packit |
ea1746 |
&execution_summary_);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The parameters are stateful, so set the state before evaluating.
|
|
Packit |
ea1746 |
if (!program_->StateVectorToParameterBlocks(state)) {
|
|
Packit |
ea1746 |
return false;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (residuals != NULL) {
|
|
Packit |
ea1746 |
VectorRef(residuals, program_->NumResiduals()).setZero();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (jacobian != NULL) {
|
|
Packit |
ea1746 |
jacobian->SetZero();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Each thread gets it's own cost and evaluate scratch space.
|
|
Packit |
ea1746 |
for (int i = 0; i < options_.num_threads; ++i) {
|
|
Packit |
ea1746 |
evaluate_scratch_[i].cost = 0.0;
|
|
Packit |
ea1746 |
if (gradient != NULL) {
|
|
Packit |
ea1746 |
VectorRef(evaluate_scratch_[i].gradient.get(),
|
|
Packit |
ea1746 |
program_->NumEffectiveParameters()).setZero();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This bool is used to disable the loop if an error is encountered
|
|
Packit |
ea1746 |
// without breaking out of it. The remaining loop iterations are still run,
|
|
Packit |
ea1746 |
// but with an empty body, and so will finish quickly.
|
|
Packit |
ea1746 |
bool abort = false;
|
|
Packit |
ea1746 |
int num_residual_blocks = program_->NumResidualBlocks();
|
|
Packit |
ea1746 |
#pragma omp parallel for num_threads(options_.num_threads)
|
|
Packit |
ea1746 |
for (int i = 0; i < num_residual_blocks; ++i) {
|
|
Packit |
ea1746 |
// Disable the loop instead of breaking, as required by OpenMP.
|
|
Packit |
ea1746 |
#pragma omp flush(abort)
|
|
Packit |
ea1746 |
if (abort) {
|
|
Packit |
ea1746 |
continue;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifdef CERES_USE_OPENMP
|
|
Packit |
ea1746 |
int thread_id = omp_get_thread_num();
|
|
Packit |
ea1746 |
#else
|
|
Packit |
ea1746 |
int thread_id = 0;
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
EvaluatePreparer* preparer = &evaluate_preparers_[thread_id];
|
|
Packit |
ea1746 |
EvaluateScratch* scratch = &evaluate_scratch_[thread_id];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Prepare block residuals if requested.
|
|
Packit |
ea1746 |
const ResidualBlock* residual_block = program_->residual_blocks()[i];
|
|
Packit |
ea1746 |
double* block_residuals = NULL;
|
|
Packit |
ea1746 |
if (residuals != NULL) {
|
|
Packit |
ea1746 |
block_residuals = residuals + residual_layout_[i];
|
|
Packit |
ea1746 |
} else if (gradient != NULL) {
|
|
Packit |
ea1746 |
block_residuals = scratch->residual_block_residuals.get();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Prepare block jacobians if requested.
|
|
Packit |
ea1746 |
double** block_jacobians = NULL;
|
|
Packit |
ea1746 |
if (jacobian != NULL || gradient != NULL) {
|
|
Packit |
ea1746 |
preparer->Prepare(residual_block,
|
|
Packit |
ea1746 |
i,
|
|
Packit |
ea1746 |
jacobian,
|
|
Packit |
ea1746 |
scratch->jacobian_block_ptrs.get());
|
|
Packit |
ea1746 |
block_jacobians = scratch->jacobian_block_ptrs.get();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Evaluate the cost, residuals, and jacobians.
|
|
Packit |
ea1746 |
double block_cost;
|
|
Packit |
ea1746 |
if (!residual_block->Evaluate(
|
|
Packit |
ea1746 |
evaluate_options.apply_loss_function,
|
|
Packit |
ea1746 |
&block_cost,
|
|
Packit |
ea1746 |
block_residuals,
|
|
Packit |
ea1746 |
block_jacobians,
|
|
Packit |
ea1746 |
scratch->residual_block_evaluate_scratch.get())) {
|
|
Packit |
ea1746 |
abort = true;
|
|
Packit |
ea1746 |
// This ensures that the OpenMP threads have a consistent view of 'abort'. Do
|
|
Packit |
ea1746 |
// the flush inside the failure case so that there is usually only one
|
|
Packit |
ea1746 |
// synchronization point per loop iteration instead of two.
|
|
Packit |
ea1746 |
#pragma omp flush(abort)
|
|
Packit |
ea1746 |
continue;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scratch->cost += block_cost;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Store the jacobians, if they were requested.
|
|
Packit |
ea1746 |
if (jacobian != NULL) {
|
|
Packit |
ea1746 |
jacobian_writer_.Write(i,
|
|
Packit |
ea1746 |
residual_layout_[i],
|
|
Packit |
ea1746 |
block_jacobians,
|
|
Packit |
ea1746 |
jacobian);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Compute and store the gradient, if it was requested.
|
|
Packit |
ea1746 |
if (gradient != NULL) {
|
|
Packit |
ea1746 |
int num_residuals = residual_block->NumResiduals();
|
|
Packit |
ea1746 |
int num_parameter_blocks = residual_block->NumParameterBlocks();
|
|
Packit |
ea1746 |
for (int j = 0; j < num_parameter_blocks; ++j) {
|
|
Packit |
ea1746 |
const ParameterBlock* parameter_block =
|
|
Packit |
ea1746 |
residual_block->parameter_blocks()[j];
|
|
Packit |
ea1746 |
if (parameter_block->IsConstant()) {
|
|
Packit |
ea1746 |
continue;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
|
|
Packit |
ea1746 |
block_jacobians[j],
|
|
Packit |
ea1746 |
num_residuals,
|
|
Packit |
ea1746 |
parameter_block->LocalSize(),
|
|
Packit |
ea1746 |
block_residuals,
|
|
Packit |
ea1746 |
scratch->gradient.get() + parameter_block->delta_offset());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (!abort) {
|
|
Packit |
ea1746 |
const int num_parameters = program_->NumEffectiveParameters();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Sum the cost and gradient (if requested) from each thread.
|
|
Packit |
ea1746 |
(*cost) = 0.0;
|
|
Packit |
ea1746 |
if (gradient != NULL) {
|
|
Packit |
ea1746 |
VectorRef(gradient, num_parameters).setZero();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
for (int i = 0; i < options_.num_threads; ++i) {
|
|
Packit |
ea1746 |
(*cost) += evaluate_scratch_[i].cost;
|
|
Packit |
ea1746 |
if (gradient != NULL) {
|
|
Packit |
ea1746 |
VectorRef(gradient, num_parameters) +=
|
|
Packit |
ea1746 |
VectorRef(evaluate_scratch_[i].gradient.get(), num_parameters);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Finalize the Jacobian if it is available.
|
|
Packit |
ea1746 |
// `num_parameters` is passed to the finalizer so that additional
|
|
Packit |
ea1746 |
// storage can be reserved for additional diagonal elements if
|
|
Packit |
ea1746 |
// necessary.
|
|
Packit |
ea1746 |
if (jacobian != NULL) {
|
|
Packit |
ea1746 |
JacobianFinalizer f;
|
|
Packit |
ea1746 |
f(jacobian, num_parameters);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
return !abort;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool Plus(const double* state,
|
|
Packit |
ea1746 |
const double* delta,
|
|
Packit |
ea1746 |
double* state_plus_delta) const {
|
|
Packit |
ea1746 |
return program_->Plus(state, delta, state_plus_delta);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int NumParameters() const {
|
|
Packit |
ea1746 |
return program_->NumParameters();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
int NumEffectiveParameters() const {
|
|
Packit |
ea1746 |
return program_->NumEffectiveParameters();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int NumResiduals() const {
|
|
Packit |
ea1746 |
return program_->NumResiduals();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual std::map<std::string, int> CallStatistics() const {
|
|
Packit |
ea1746 |
return execution_summary_.calls();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual std::map<std::string, double> TimeStatistics() const {
|
|
Packit |
ea1746 |
return execution_summary_.times();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
// Per-thread scratch space needed to evaluate and store each residual block.
|
|
Packit |
ea1746 |
struct EvaluateScratch {
|
|
Packit |
ea1746 |
void Init(int max_parameters_per_residual_block,
|
|
Packit |
ea1746 |
int max_scratch_doubles_needed_for_evaluate,
|
|
Packit |
ea1746 |
int max_residuals_per_residual_block,
|
|
Packit |
ea1746 |
int num_parameters) {
|
|
Packit |
ea1746 |
residual_block_evaluate_scratch.reset(
|
|
Packit |
ea1746 |
new double[max_scratch_doubles_needed_for_evaluate]);
|
|
Packit |
ea1746 |
gradient.reset(new double[num_parameters]);
|
|
Packit |
ea1746 |
VectorRef(gradient.get(), num_parameters).setZero();
|
|
Packit |
ea1746 |
residual_block_residuals.reset(
|
|
Packit |
ea1746 |
new double[max_residuals_per_residual_block]);
|
|
Packit |
ea1746 |
jacobian_block_ptrs.reset(
|
|
Packit |
ea1746 |
new double*[max_parameters_per_residual_block]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double cost;
|
|
Packit |
ea1746 |
scoped_array<double> residual_block_evaluate_scratch;
|
|
Packit |
ea1746 |
// The gradient in the local parameterization.
|
|
Packit |
ea1746 |
scoped_array<double> gradient;
|
|
Packit |
ea1746 |
// Enough space to store the residual for the largest residual block.
|
|
Packit |
ea1746 |
scoped_array<double> residual_block_residuals;
|
|
Packit |
ea1746 |
scoped_array<double*> jacobian_block_ptrs;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
static void BuildResidualLayout(const Program& program,
|
|
Packit |
ea1746 |
std::vector<int>* residual_layout) {
|
|
Packit |
ea1746 |
const std::vector<ResidualBlock*>& residual_blocks =
|
|
Packit |
ea1746 |
program.residual_blocks();
|
|
Packit |
ea1746 |
residual_layout->resize(program.NumResidualBlocks());
|
|
Packit |
ea1746 |
int residual_pos = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < residual_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
const int num_residuals = residual_blocks[i]->NumResiduals();
|
|
Packit |
ea1746 |
(*residual_layout)[i] = residual_pos;
|
|
Packit |
ea1746 |
residual_pos += num_residuals;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Create scratch space for each thread evaluating the program.
|
|
Packit |
ea1746 |
static EvaluateScratch* CreateEvaluatorScratch(const Program& program,
|
|
Packit |
ea1746 |
int num_threads) {
|
|
Packit |
ea1746 |
int max_parameters_per_residual_block =
|
|
Packit |
ea1746 |
program.MaxParametersPerResidualBlock();
|
|
Packit |
ea1746 |
int max_scratch_doubles_needed_for_evaluate =
|
|
Packit |
ea1746 |
program.MaxScratchDoublesNeededForEvaluate();
|
|
Packit |
ea1746 |
int max_residuals_per_residual_block =
|
|
Packit |
ea1746 |
program.MaxResidualsPerResidualBlock();
|
|
Packit |
ea1746 |
int num_parameters = program.NumEffectiveParameters();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EvaluateScratch* evaluate_scratch = new EvaluateScratch[num_threads];
|
|
Packit |
ea1746 |
for (int i = 0; i < num_threads; i++) {
|
|
Packit |
ea1746 |
evaluate_scratch[i].Init(max_parameters_per_residual_block,
|
|
Packit |
ea1746 |
max_scratch_doubles_needed_for_evaluate,
|
|
Packit |
ea1746 |
max_residuals_per_residual_block,
|
|
Packit |
ea1746 |
num_parameters);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
return evaluate_scratch;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Evaluator::Options options_;
|
|
Packit |
ea1746 |
Program* program_;
|
|
Packit |
ea1746 |
JacobianWriter jacobian_writer_;
|
|
Packit |
ea1746 |
scoped_array<EvaluatePreparer> evaluate_preparers_;
|
|
Packit |
ea1746 |
scoped_array<EvaluateScratch> evaluate_scratch_;
|
|
Packit |
ea1746 |
std::vector<int> residual_layout_;
|
|
Packit |
ea1746 |
::ceres::internal::ExecutionSummary execution_summary_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_PROGRAM_EVALUATOR_H_
|