Blame internal/ceres/block_random_access_matrix.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
// Interface for matrices that allow block based random access.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
Packit ea1746
#define CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
Packit ea1746
Packit ea1746
#include "ceres/mutex.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// A matrix implementing the BlockRandomAccessMatrix interface is a
Packit ea1746
// matrix whose rows and columns are divided into blocks. For example
Packit ea1746
// the matrix A:
Packit ea1746
//
Packit ea1746
//            3     4      5
Packit ea1746
//  A =  5 [c_11  c_12  c_13]
Packit ea1746
//       4 [c_21  c_22  c_23]
Packit ea1746
//
Packit ea1746
// has row blocks of size 5 and 4, and column blocks of size 3, 4 and
Packit ea1746
// 5. It has six cells corresponding to the six row-column block
Packit ea1746
// combinations.
Packit ea1746
//
Packit ea1746
// BlockRandomAccessMatrix objects provide access to cells c_ij using
Packit ea1746
// the GetCell method. when a cell is present, GetCell will return a
Packit ea1746
// CellInfo object containing a pointer to an array which contains the
Packit ea1746
// cell as a submatrix and a mutex that guards this submatrix. If the
Packit ea1746
// user is accessing the matrix concurrently, it is his responsibility
Packit ea1746
// to use the mutex to exclude other writers from writing to the cell
Packit ea1746
// concurrently.
Packit ea1746
//
Packit ea1746
// There is no requirement that all cells be present, i.e. the matrix
Packit ea1746
// itself can be block sparse. When a cell is not present, the GetCell
Packit ea1746
// method will return a NULL pointer.
Packit ea1746
//
Packit ea1746
// There is no requirement about how the cells are stored beyond that
Packit ea1746
// form a dense submatrix of a larger dense matrix. Like everywhere
Packit ea1746
// else in Ceres, RowMajor storage assumed.
Packit ea1746
//
Packit ea1746
// Example usage:
Packit ea1746
//
Packit ea1746
//  BlockRandomAccessMatrix* A = new BlockRandomAccessMatrixSubClass(...)
Packit ea1746
//
Packit ea1746
//  int row, col, row_stride, col_stride;
Packit ea1746
//  CellInfo* cell = A->GetCell(row_block_id, col_block_id,
Packit ea1746
//                              &row, &col,
Packit ea1746
//                              &row_stride, &col_stride);
Packit ea1746
//
Packit ea1746
//  if (cell != NULL) {
Packit ea1746
//     MatrixRef m(cell->values, row_stride, col_stride);
Packit ea1746
//     CeresMutexLock l(&cell->m);
Packit ea1746
//     m.block(row, col, row_block_size, col_block_size) = ...
Packit ea1746
//  }
Packit ea1746
Packit ea1746
// Structure to carry a pointer to the array containing a cell and the
Packit ea1746
// Mutex guarding it.
Packit ea1746
struct CellInfo {
Packit ea1746
  CellInfo()
Packit ea1746
      : values(NULL) {
Packit ea1746
  }
Packit ea1746
Packit ea1746
  explicit CellInfo(double* ptr)
Packit ea1746
      : values(ptr) {
Packit ea1746
  }
Packit ea1746
Packit ea1746
  double* values;
Packit ea1746
  Mutex m;
Packit ea1746
};
Packit ea1746
Packit ea1746
class BlockRandomAccessMatrix {
Packit ea1746
 public:
Packit ea1746
  virtual ~BlockRandomAccessMatrix();
Packit ea1746
Packit ea1746
  // If the cell (row_block_id, col_block_id) is present, then return
Packit ea1746
  // a CellInfo with a pointer to the dense matrix containing it,
Packit ea1746
  // otherwise return NULL. The dense matrix containing this cell has
Packit ea1746
  // size row_stride, col_stride and the cell is located at position
Packit ea1746
  // (row, col) within this matrix.
Packit ea1746
  //
Packit ea1746
  // The size of the cell is row_block_size x col_block_size is
Packit ea1746
  // assumed known to the caller. row_block_size less than or equal to
Packit ea1746
  // row_stride and col_block_size is upper bounded by col_stride.
Packit ea1746
  virtual CellInfo* GetCell(int row_block_id,
Packit ea1746
                            int col_block_id,
Packit ea1746
                            int* row,
Packit ea1746
                            int* col,
Packit ea1746
                            int* row_stride,
Packit ea1746
                            int* col_stride) = 0;
Packit ea1746
Packit ea1746
  // Zero out the values of the array. The structure of the matrix
Packit ea1746
  // (size and sparsity) is preserved.
Packit ea1746
  virtual void SetZero() = 0;
Packit ea1746
Packit ea1746
  // Number of scalar rows and columns in the matrix, i.e the sum of
Packit ea1746
  // all row blocks and column block sizes respectively.
Packit ea1746
  virtual int num_rows() const = 0;
Packit ea1746
  virtual int num_cols() const = 0;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_