Blame internal/ceres/preconditioner.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: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_PRECONDITIONER_H_
Packit ea1746
#define CERES_INTERNAL_PRECONDITIONER_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/casts.h"
Packit ea1746
#include "ceres/compressed_row_sparse_matrix.h"
Packit ea1746
#include "ceres/linear_operator.h"
Packit ea1746
#include "ceres/sparse_matrix.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class BlockSparseMatrix;
Packit ea1746
class SparseMatrix;
Packit ea1746
Packit ea1746
class Preconditioner : public LinearOperator {
Packit ea1746
 public:
Packit ea1746
  struct Options {
Packit ea1746
    Options()
Packit ea1746
        : type(JACOBI),
Packit ea1746
          visibility_clustering_type(CANONICAL_VIEWS),
Packit ea1746
          sparse_linear_algebra_library_type(SUITE_SPARSE),
Packit ea1746
          num_threads(1),
Packit ea1746
          row_block_size(Eigen::Dynamic),
Packit ea1746
          e_block_size(Eigen::Dynamic),
Packit ea1746
          f_block_size(Eigen::Dynamic) {
Packit ea1746
    }
Packit ea1746
Packit ea1746
    PreconditionerType type;
Packit ea1746
    VisibilityClusteringType visibility_clustering_type;
Packit ea1746
    SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type;
Packit ea1746
Packit ea1746
    // If possible, how many threads the preconditioner can use.
Packit ea1746
    int num_threads;
Packit ea1746
Packit ea1746
    // Hints about the order in which the parameter blocks should be
Packit ea1746
    // eliminated by the linear solver.
Packit ea1746
    //
Packit ea1746
    // For example if elimination_groups is a vector of size k, then
Packit ea1746
    // the linear solver is informed that it should eliminate the
Packit ea1746
    // parameter blocks 0 ... elimination_groups[0] - 1 first, and
Packit ea1746
    // then elimination_groups[0] ... elimination_groups[1] - 1 and so
Packit ea1746
    // on. Within each elimination group, the linear solver is free to
Packit ea1746
    // choose how the parameter blocks are ordered. Different linear
Packit ea1746
    // solvers have differing requirements on elimination_groups.
Packit ea1746
    //
Packit ea1746
    // The most common use is for Schur type solvers, where there
Packit ea1746
    // should be at least two elimination groups and the first
Packit ea1746
    // elimination group must form an independent set in the normal
Packit ea1746
    // equations. The first elimination group corresponds to the
Packit ea1746
    // num_eliminate_blocks in the Schur type solvers.
Packit ea1746
    std::vector<int> elimination_groups;
Packit ea1746
Packit ea1746
    // If the block sizes in a BlockSparseMatrix are fixed, then in
Packit ea1746
    // some cases the Schur complement based solvers can detect and
Packit ea1746
    // specialize on them.
Packit ea1746
    //
Packit ea1746
    // It is expected that these parameters are set programmatically
Packit ea1746
    // rather than manually.
Packit ea1746
    //
Packit ea1746
    // Please see schur_complement_solver.h and schur_eliminator.h for
Packit ea1746
    // more details.
Packit ea1746
    int row_block_size;
Packit ea1746
    int e_block_size;
Packit ea1746
    int f_block_size;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // If the optimization problem is such that there are no remaining
Packit ea1746
  // e-blocks, ITERATIVE_SCHUR with a Schur type preconditioner cannot
Packit ea1746
  // be used. This function returns JACOBI if a preconditioner for
Packit ea1746
  // ITERATIVE_SCHUR is used. The input preconditioner_type is
Packit ea1746
  // returned otherwise.
Packit ea1746
  static PreconditionerType PreconditionerForZeroEBlocks(
Packit ea1746
      PreconditionerType preconditioner_type);
Packit ea1746
Packit ea1746
  virtual ~Preconditioner();
Packit ea1746
Packit ea1746
  // Update the numerical value of the preconditioner for the linear
Packit ea1746
  // system:
Packit ea1746
  //
Packit ea1746
  //  |   A   | x = |b|
Packit ea1746
  //  |diag(D)|     |0|
Packit ea1746
  //
Packit ea1746
  // for some vector b. It is important that the matrix A have the
Packit ea1746
  // same block structure as the one used to construct this object.
Packit ea1746
  //
Packit ea1746
  // D can be NULL, in which case its interpreted as a diagonal matrix
Packit ea1746
  // of size zero.
Packit ea1746
  virtual bool Update(const LinearOperator& A, const double* D) = 0;
Packit ea1746
Packit ea1746
  // LinearOperator interface. Since the operator is symmetric,
Packit ea1746
  // LeftMultiply and num_cols are just calls to RightMultiply and
Packit ea1746
  // num_rows respectively. Update() must be called before
Packit ea1746
  // RightMultiply can be called.
Packit ea1746
  virtual void RightMultiply(const double* x, double* y) const = 0;
Packit ea1746
  virtual void LeftMultiply(const double* x, double* y) const {
Packit ea1746
    return RightMultiply(x, y);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  virtual int num_rows() const = 0;
Packit ea1746
  virtual int num_cols() const {
Packit ea1746
    return num_rows();
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
// This templated subclass of Preconditioner serves as a base class for
Packit ea1746
// other preconditioners that depend on the particular matrix layout of
Packit ea1746
// the underlying linear operator.
Packit ea1746
template <typename MatrixType>
Packit ea1746
class TypedPreconditioner : public Preconditioner {
Packit ea1746
 public:
Packit ea1746
  virtual ~TypedPreconditioner() {}
Packit ea1746
  virtual bool Update(const LinearOperator& A, const double* D) {
Packit ea1746
    return UpdateImpl(*down_cast<const MatrixType*>(&A), D);
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual bool UpdateImpl(const MatrixType& A, const double* D) = 0;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Preconditioners that depend on acccess to the low level structure
Packit ea1746
// of a SparseMatrix.
Packit ea1746
typedef TypedPreconditioner<SparseMatrix>              SparseMatrixPreconditioner;               // NOLINT
Packit ea1746
typedef TypedPreconditioner<BlockSparseMatrix>         BlockSparseMatrixPreconditioner;          // NOLINT
Packit ea1746
typedef TypedPreconditioner<CompressedRowSparseMatrix> CompressedRowSparseMatrixPreconditioner;  // NOLINT
Packit ea1746
Packit ea1746
// Wrap a SparseMatrix object as a preconditioner.
Packit ea1746
class SparseMatrixPreconditionerWrapper : public SparseMatrixPreconditioner {
Packit ea1746
 public:
Packit ea1746
  // Wrapper does NOT take ownership of the matrix pointer.
Packit ea1746
  explicit SparseMatrixPreconditionerWrapper(const SparseMatrix* matrix);
Packit ea1746
  virtual ~SparseMatrixPreconditionerWrapper();
Packit ea1746
Packit ea1746
  // Preconditioner interface
Packit ea1746
  virtual void RightMultiply(const double* x, double* y) const;
Packit ea1746
  virtual int num_rows() const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual bool UpdateImpl(const SparseMatrix& A, const double* D);
Packit ea1746
  const SparseMatrix* matrix_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_PRECONDITIONER_H_