blob: 66390d71ef89fbc59cc728b4ac3eb86c4bd589dd [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070038#include "ceres/internal/export.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080039
Austin Schuh3de38b02024-06-25 18:25:10 -070040namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080041
42// A matrix implementing the BlockRandomAccessMatrix interface is a
43// matrix whose rows and columns are divided into blocks. For example
44// the matrix A:
45//
46// 3 4 5
47// A = 5 [c_11 c_12 c_13]
48// 4 [c_21 c_22 c_23]
49//
50// has row blocks of size 5 and 4, and column blocks of size 3, 4 and
51// 5. It has six cells corresponding to the six row-column block
52// combinations.
53//
54// BlockRandomAccessMatrix objects provide access to cells c_ij using
55// the GetCell method. when a cell is present, GetCell will return a
56// CellInfo object containing a pointer to an array which contains the
57// cell as a submatrix and a mutex that guards this submatrix. If the
58// user is accessing the matrix concurrently, it is his responsibility
59// to use the mutex to exclude other writers from writing to the cell
60// concurrently.
61//
62// There is no requirement that all cells be present, i.e. the matrix
63// itself can be block sparse. When a cell is not present, the GetCell
Austin Schuh3de38b02024-06-25 18:25:10 -070064// method will return a nullptr pointer.
Austin Schuh70cc9552019-01-21 19:46:48 -080065//
66// There is no requirement about how the cells are stored beyond that
67// form a dense submatrix of a larger dense matrix. Like everywhere
68// else in Ceres, RowMajor storage assumed.
69//
70// Example usage:
71//
72// BlockRandomAccessMatrix* A = new BlockRandomAccessMatrixSubClass(...)
73//
74// int row, col, row_stride, col_stride;
75// CellInfo* cell = A->GetCell(row_block_id, col_block_id,
76// &row, &col,
77// &row_stride, &col_stride);
78//
Austin Schuh3de38b02024-06-25 18:25:10 -070079// if (cell != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080080// MatrixRef m(cell->values, row_stride, col_stride);
81// std::lock_guard<std::mutex> l(&cell->m);
82// m.block(row, col, row_block_size, col_block_size) = ...
83// }
84
85// Structure to carry a pointer to the array containing a cell and the
86// mutex guarding it.
Austin Schuh3de38b02024-06-25 18:25:10 -070087struct CERES_NO_EXPORT CellInfo {
88 CellInfo() = default;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080089 explicit CellInfo(double* values) : values(values) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080090
Austin Schuh3de38b02024-06-25 18:25:10 -070091 double* values{nullptr};
Austin Schuh70cc9552019-01-21 19:46:48 -080092 std::mutex m;
93};
94
Austin Schuh3de38b02024-06-25 18:25:10 -070095class CERES_NO_EXPORT BlockRandomAccessMatrix {
Austin Schuh70cc9552019-01-21 19:46:48 -080096 public:
97 virtual ~BlockRandomAccessMatrix();
98
99 // If the cell (row_block_id, col_block_id) is present, then return
100 // a CellInfo with a pointer to the dense matrix containing it,
Austin Schuh3de38b02024-06-25 18:25:10 -0700101 // otherwise return nullptr. The dense matrix containing this cell has
Austin Schuh70cc9552019-01-21 19:46:48 -0800102 // size row_stride, col_stride and the cell is located at position
103 // (row, col) within this matrix.
104 //
105 // The size of the cell is row_block_size x col_block_size is
106 // assumed known to the caller. row_block_size less than or equal to
107 // row_stride and col_block_size is upper bounded by col_stride.
108 virtual CellInfo* GetCell(int row_block_id,
109 int col_block_id,
110 int* row,
111 int* col,
112 int* row_stride,
113 int* col_stride) = 0;
114
115 // Zero out the values of the array. The structure of the matrix
116 // (size and sparsity) is preserved.
117 virtual void SetZero() = 0;
118
119 // Number of scalar rows and columns in the matrix, i.e the sum of
120 // all row blocks and column block sizes respectively.
121 virtual int num_rows() const = 0;
122 virtual int num_cols() const = 0;
123};
124
Austin Schuh3de38b02024-06-25 18:25:10 -0700125} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800126
127#endif // CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_