Blame internal/ceres/block_jacobian_writer.h

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
// A jacobian writer that writes to block sparse matrices. The "writer" name is
Packit ea1746
// misleading, since the Write() operation on the block jacobian writer does not
Packit ea1746
// write anything. Instead, the Prepare() method on the BlockEvaluatePreparers
Packit ea1746
// makes a jacobians array which has direct pointers into the block sparse
Packit ea1746
// jacobian. When the cost function is evaluated, the jacobian blocks get placed
Packit ea1746
// directly in their final location.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_BLOCK_JACOBIAN_WRITER_H_
Packit ea1746
#define CERES_INTERNAL_BLOCK_JACOBIAN_WRITER_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/evaluator.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class BlockEvaluatePreparer;
Packit ea1746
class Program;
Packit ea1746
class SparseMatrix;
Packit ea1746
Packit ea1746
// TODO(sameeragarwal): This class needs documemtation.
Packit ea1746
class BlockJacobianWriter {
Packit ea1746
 public:
Packit ea1746
  BlockJacobianWriter(const Evaluator::Options& options,
Packit ea1746
                      Program* program);
Packit ea1746
Packit ea1746
  // JacobianWriter interface.
Packit ea1746
Packit ea1746
  // Create evaluate prepareres that point directly into the final jacobian.
Packit ea1746
  // This makes the final Write() a nop.
Packit ea1746
  BlockEvaluatePreparer* CreateEvaluatePreparers(int num_threads);
Packit ea1746
Packit ea1746
  SparseMatrix* CreateJacobian() const;
Packit ea1746
Packit ea1746
  void Write(int /* residual_id */,
Packit ea1746
             int /* residual_offset */,
Packit ea1746
             double** /* jacobians */,
Packit ea1746
             SparseMatrix* /* jacobian */) {
Packit ea1746
    // This is a noop since the blocks were written directly into their final
Packit ea1746
    // position by the outside evaluate call, thanks to the jacobians array
Packit ea1746
    // prepared by the BlockEvaluatePreparers.
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  Program* program_;
Packit ea1746
Packit ea1746
  // Stores the position of each residual / parameter jacobian.
Packit ea1746
  //
Packit ea1746
  // The block sparse matrix that this writer writes to is stored as a set of
Packit ea1746
  // contiguos dense blocks, one after each other; see BlockSparseMatrix. The
Packit ea1746
  // "double* values_" member of the block sparse matrix contains all of these
Packit ea1746
  // blocks. Given a pointer to the first element of a block and the size of
Packit ea1746
  // that block, it's possible to write to it.
Packit ea1746
  //
Packit ea1746
  // In the case of a block sparse jacobian, the jacobian writer needs a way to
Packit ea1746
  // find the offset in the values_ array of each residual/parameter jacobian
Packit ea1746
  // block.
Packit ea1746
  //
Packit ea1746
  // That is the purpose of jacobian_layout_.
Packit ea1746
  //
Packit ea1746
  // In particular, jacobian_layout_[i][j] is the offset in the values_ array of
Packit ea1746
  // the derivative of residual block i with respect to the parameter block at
Packit ea1746
  // active argument position j.
Packit ea1746
  //
Packit ea1746
  // The active qualifier means that non-active parameters do not count. Care
Packit ea1746
  // must be taken when indexing into jacobian_layout_ to account for this.
Packit ea1746
  // Consider a single residual example:
Packit ea1746
  //
Packit ea1746
  //   r(x, y, z)
Packit ea1746
  //
Packit ea1746
  // with r in R^3, x in R^4, y in R^2, and z in R^5.
Packit ea1746
  // Take y as a constant (non-active) parameter.
Packit ea1746
  // Take r as residual number 0.
Packit ea1746
  //
Packit ea1746
  // In this case, the active arguments are only (x, z), so the active argument
Packit ea1746
  // position for x is 0, and the active argument position for z is 1. This is
Packit ea1746
  // similar to thinking of r as taking only 2 parameters:
Packit ea1746
  //
Packit ea1746
  //   r(x, z)
Packit ea1746
  //
Packit ea1746
  // There are only 2 jacobian blocks: dr/dx and dr/dz. jacobian_layout_ would
Packit ea1746
  // have the following contents:
Packit ea1746
  //
Packit ea1746
  //   jacobian_layout_[0] = { 0, 12 }
Packit ea1746
  //
Packit ea1746
  // which indicates that dr/dx is located at values_[0], and dr/dz is at
Packit ea1746
  // values_[12]. See BlockEvaluatePreparer::Prepare()'s comments about 'j'.
Packit ea1746
  std::vector<int*> jacobian_layout_;
Packit ea1746
Packit ea1746
  // The pointers in jacobian_layout_ point directly into this vector.
Packit ea1746
  std::vector<int> jacobian_layout_storage_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_BLOCK_JACOBIAN_WRITER_H_