blob: f190622eafead6bf68ae524399d58ca87537e2d1 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
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: sameeragarwal@google.com (Sameer Agarwal)
30//
31// Interface for matrices that allow block based random access.
32
33#ifndef CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
34#define CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
35
36#include <mutex>
37
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038#include "ceres/internal/port.h"
39
Austin Schuh70cc9552019-01-21 19:46:48 -080040namespace ceres {
41namespace internal {
42
43// A matrix implementing the BlockRandomAccessMatrix interface is a
44// matrix whose rows and columns are divided into blocks. For example
45// the matrix A:
46//
47// 3 4 5
48// A = 5 [c_11 c_12 c_13]
49// 4 [c_21 c_22 c_23]
50//
51// has row blocks of size 5 and 4, and column blocks of size 3, 4 and
52// 5. It has six cells corresponding to the six row-column block
53// combinations.
54//
55// BlockRandomAccessMatrix objects provide access to cells c_ij using
56// the GetCell method. when a cell is present, GetCell will return a
57// CellInfo object containing a pointer to an array which contains the
58// cell as a submatrix and a mutex that guards this submatrix. If the
59// user is accessing the matrix concurrently, it is his responsibility
60// to use the mutex to exclude other writers from writing to the cell
61// concurrently.
62//
63// There is no requirement that all cells be present, i.e. the matrix
64// itself can be block sparse. When a cell is not present, the GetCell
65// method will return a NULL pointer.
66//
67// There is no requirement about how the cells are stored beyond that
68// form a dense submatrix of a larger dense matrix. Like everywhere
69// else in Ceres, RowMajor storage assumed.
70//
71// Example usage:
72//
73// BlockRandomAccessMatrix* A = new BlockRandomAccessMatrixSubClass(...)
74//
75// int row, col, row_stride, col_stride;
76// CellInfo* cell = A->GetCell(row_block_id, col_block_id,
77// &row, &col,
78// &row_stride, &col_stride);
79//
80// if (cell != NULL) {
81// MatrixRef m(cell->values, row_stride, col_stride);
82// std::lock_guard<std::mutex> l(&cell->m);
83// m.block(row, col, row_block_size, col_block_size) = ...
84// }
85
86// Structure to carry a pointer to the array containing a cell and the
87// mutex guarding it.
88struct CellInfo {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080089 CellInfo() : values(nullptr) {}
90 explicit CellInfo(double* values) : values(values) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080091
92 double* values;
93 std::mutex m;
94};
95
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080096class CERES_EXPORT_INTERNAL BlockRandomAccessMatrix {
Austin Schuh70cc9552019-01-21 19:46:48 -080097 public:
98 virtual ~BlockRandomAccessMatrix();
99
100 // If the cell (row_block_id, col_block_id) is present, then return
101 // a CellInfo with a pointer to the dense matrix containing it,
102 // otherwise return NULL. The dense matrix containing this cell has
103 // size row_stride, col_stride and the cell is located at position
104 // (row, col) within this matrix.
105 //
106 // The size of the cell is row_block_size x col_block_size is
107 // assumed known to the caller. row_block_size less than or equal to
108 // row_stride and col_block_size is upper bounded by col_stride.
109 virtual CellInfo* GetCell(int row_block_id,
110 int col_block_id,
111 int* row,
112 int* col,
113 int* row_stride,
114 int* col_stride) = 0;
115
116 // Zero out the values of the array. The structure of the matrix
117 // (size and sparsity) is preserved.
118 virtual void SetZero() = 0;
119
120 // Number of scalar rows and columns in the matrix, i.e the sum of
121 // all row blocks and column block sizes respectively.
122 virtual int num_rows() const = 0;
123 virtual int num_cols() const = 0;
124};
125
126} // namespace internal
127} // namespace ceres
128
129#endif // CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_