blob: 5efd2e1ecfba95bc1cd888b6d65b7be5b985666a [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#include "ceres/block_sparse_matrix.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include <algorithm>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034#include <cstddef>
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/block_structure.h"
38#include "ceres/internal/eigen.h"
39#include "ceres/random.h"
40#include "ceres/small_blas.h"
41#include "ceres/triplet_sparse_matrix.h"
42#include "glog/logging.h"
43
44namespace ceres {
45namespace internal {
46
47using std::vector;
48
49BlockSparseMatrix::~BlockSparseMatrix() {}
50
51BlockSparseMatrix::BlockSparseMatrix(
52 CompressedRowBlockStructure* block_structure)
53 : num_rows_(0),
54 num_cols_(0),
55 num_nonzeros_(0),
56 block_structure_(block_structure) {
57 CHECK(block_structure_ != nullptr);
58
59 // Count the number of columns in the matrix.
60 for (int i = 0; i < block_structure_->cols.size(); ++i) {
61 num_cols_ += block_structure_->cols[i].size;
62 }
63
64 // Count the number of non-zero entries and the number of rows in
65 // the matrix.
66 for (int i = 0; i < block_structure_->rows.size(); ++i) {
67 int row_block_size = block_structure_->rows[i].block.size;
68 num_rows_ += row_block_size;
69
70 const vector<Cell>& cells = block_structure_->rows[i].cells;
71 for (int j = 0; j < cells.size(); ++j) {
72 int col_block_id = cells[j].block_id;
73 int col_block_size = block_structure_->cols[col_block_id].size;
74 num_nonzeros_ += col_block_size * row_block_size;
75 }
76 }
77
78 CHECK_GE(num_rows_, 0);
79 CHECK_GE(num_cols_, 0);
80 CHECK_GE(num_nonzeros_, 0);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080081 VLOG(2) << "Allocating values array with " << num_nonzeros_ * sizeof(double)
82 << " bytes."; // NOLINT
Austin Schuh70cc9552019-01-21 19:46:48 -080083 values_.reset(new double[num_nonzeros_]);
84 max_num_nonzeros_ = num_nonzeros_;
85 CHECK(values_ != nullptr);
86}
87
88void BlockSparseMatrix::SetZero() {
89 std::fill(values_.get(), values_.get() + num_nonzeros_, 0.0);
90}
91
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080092void BlockSparseMatrix::RightMultiply(const double* x, double* y) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080093 CHECK(x != nullptr);
94 CHECK(y != nullptr);
95
96 for (int i = 0; i < block_structure_->rows.size(); ++i) {
97 int row_block_pos = block_structure_->rows[i].block.position;
98 int row_block_size = block_structure_->rows[i].block.size;
99 const vector<Cell>& cells = block_structure_->rows[i].cells;
100 for (int j = 0; j < cells.size(); ++j) {
101 int col_block_id = cells[j].block_id;
102 int col_block_size = block_structure_->cols[col_block_id].size;
103 int col_block_pos = block_structure_->cols[col_block_id].position;
104 MatrixVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105 values_.get() + cells[j].position,
106 row_block_size,
107 col_block_size,
Austin Schuh70cc9552019-01-21 19:46:48 -0800108 x + col_block_pos,
109 y + row_block_pos);
110 }
111 }
112}
113
114void BlockSparseMatrix::LeftMultiply(const double* x, double* y) const {
115 CHECK(x != nullptr);
116 CHECK(y != nullptr);
117
118 for (int i = 0; i < block_structure_->rows.size(); ++i) {
119 int row_block_pos = block_structure_->rows[i].block.position;
120 int row_block_size = block_structure_->rows[i].block.size;
121 const vector<Cell>& cells = block_structure_->rows[i].cells;
122 for (int j = 0; j < cells.size(); ++j) {
123 int col_block_id = cells[j].block_id;
124 int col_block_size = block_structure_->cols[col_block_id].size;
125 int col_block_pos = block_structure_->cols[col_block_id].position;
126 MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800127 values_.get() + cells[j].position,
128 row_block_size,
129 col_block_size,
Austin Schuh70cc9552019-01-21 19:46:48 -0800130 x + row_block_pos,
131 y + col_block_pos);
132 }
133 }
134}
135
136void BlockSparseMatrix::SquaredColumnNorm(double* x) const {
137 CHECK(x != nullptr);
138 VectorRef(x, num_cols_).setZero();
139 for (int i = 0; i < block_structure_->rows.size(); ++i) {
140 int row_block_size = block_structure_->rows[i].block.size;
141 const vector<Cell>& cells = block_structure_->rows[i].cells;
142 for (int j = 0; j < cells.size(); ++j) {
143 int col_block_id = cells[j].block_id;
144 int col_block_size = block_structure_->cols[col_block_id].size;
145 int col_block_pos = block_structure_->cols[col_block_id].position;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800146 const MatrixRef m(
147 values_.get() + cells[j].position, row_block_size, col_block_size);
Austin Schuh70cc9552019-01-21 19:46:48 -0800148 VectorRef(x + col_block_pos, col_block_size) += m.colwise().squaredNorm();
149 }
150 }
151}
152
153void BlockSparseMatrix::ScaleColumns(const double* scale) {
154 CHECK(scale != nullptr);
155
156 for (int i = 0; i < block_structure_->rows.size(); ++i) {
157 int row_block_size = block_structure_->rows[i].block.size;
158 const vector<Cell>& cells = block_structure_->rows[i].cells;
159 for (int j = 0; j < cells.size(); ++j) {
160 int col_block_id = cells[j].block_id;
161 int col_block_size = block_structure_->cols[col_block_id].size;
162 int col_block_pos = block_structure_->cols[col_block_id].position;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800163 MatrixRef m(
164 values_.get() + cells[j].position, row_block_size, col_block_size);
Austin Schuh70cc9552019-01-21 19:46:48 -0800165 m *= ConstVectorRef(scale + col_block_pos, col_block_size).asDiagonal();
166 }
167 }
168}
169
170void BlockSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
171 CHECK(dense_matrix != nullptr);
172
173 dense_matrix->resize(num_rows_, num_cols_);
174 dense_matrix->setZero();
175 Matrix& m = *dense_matrix;
176
177 for (int i = 0; i < block_structure_->rows.size(); ++i) {
178 int row_block_pos = block_structure_->rows[i].block.position;
179 int row_block_size = block_structure_->rows[i].block.size;
180 const vector<Cell>& cells = block_structure_->rows[i].cells;
181 for (int j = 0; j < cells.size(); ++j) {
182 int col_block_id = cells[j].block_id;
183 int col_block_size = block_structure_->cols[col_block_id].size;
184 int col_block_pos = block_structure_->cols[col_block_id].position;
185 int jac_pos = cells[j].position;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800186 m.block(row_block_pos, col_block_pos, row_block_size, col_block_size) +=
187 MatrixRef(values_.get() + jac_pos, row_block_size, col_block_size);
Austin Schuh70cc9552019-01-21 19:46:48 -0800188 }
189 }
190}
191
192void BlockSparseMatrix::ToTripletSparseMatrix(
193 TripletSparseMatrix* matrix) const {
194 CHECK(matrix != nullptr);
195
196 matrix->Reserve(num_nonzeros_);
197 matrix->Resize(num_rows_, num_cols_);
198 matrix->SetZero();
199
200 for (int i = 0; i < block_structure_->rows.size(); ++i) {
201 int row_block_pos = block_structure_->rows[i].block.position;
202 int row_block_size = block_structure_->rows[i].block.size;
203 const vector<Cell>& cells = block_structure_->rows[i].cells;
204 for (int j = 0; j < cells.size(); ++j) {
205 int col_block_id = cells[j].block_id;
206 int col_block_size = block_structure_->cols[col_block_id].size;
207 int col_block_pos = block_structure_->cols[col_block_id].position;
208 int jac_pos = cells[j].position;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800209 for (int r = 0; r < row_block_size; ++r) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800210 for (int c = 0; c < col_block_size; ++c, ++jac_pos) {
211 matrix->mutable_rows()[jac_pos] = row_block_pos + r;
212 matrix->mutable_cols()[jac_pos] = col_block_pos + c;
213 matrix->mutable_values()[jac_pos] = values_[jac_pos];
214 }
215 }
216 }
217 }
218 matrix->set_num_nonzeros(num_nonzeros_);
219}
220
221// Return a pointer to the block structure. We continue to hold
222// ownership of the object though.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800223const CompressedRowBlockStructure* BlockSparseMatrix::block_structure() const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800224 return block_structure_.get();
225}
226
227void BlockSparseMatrix::ToTextFile(FILE* file) const {
228 CHECK(file != nullptr);
229 for (int i = 0; i < block_structure_->rows.size(); ++i) {
230 const int row_block_pos = block_structure_->rows[i].block.position;
231 const int row_block_size = block_structure_->rows[i].block.size;
232 const vector<Cell>& cells = block_structure_->rows[i].cells;
233 for (int j = 0; j < cells.size(); ++j) {
234 const int col_block_id = cells[j].block_id;
235 const int col_block_size = block_structure_->cols[col_block_id].size;
236 const int col_block_pos = block_structure_->cols[col_block_id].position;
237 int jac_pos = cells[j].position;
238 for (int r = 0; r < row_block_size; ++r) {
239 for (int c = 0; c < col_block_size; ++c) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800240 fprintf(file,
241 "% 10d % 10d %17f\n",
Austin Schuh70cc9552019-01-21 19:46:48 -0800242 row_block_pos + r,
243 col_block_pos + c,
244 values_[jac_pos++]);
245 }
246 }
247 }
248 }
249}
250
251BlockSparseMatrix* BlockSparseMatrix::CreateDiagonalMatrix(
252 const double* diagonal, const std::vector<Block>& column_blocks) {
253 // Create the block structure for the diagonal matrix.
254 CompressedRowBlockStructure* bs = new CompressedRowBlockStructure();
255 bs->cols = column_blocks;
256 int position = 0;
257 bs->rows.resize(column_blocks.size(), CompressedRow(1));
258 for (int i = 0; i < column_blocks.size(); ++i) {
259 CompressedRow& row = bs->rows[i];
260 row.block = column_blocks[i];
261 Cell& cell = row.cells[0];
262 cell.block_id = i;
263 cell.position = position;
264 position += row.block.size * row.block.size;
265 }
266
267 // Create the BlockSparseMatrix with the given block structure.
268 BlockSparseMatrix* matrix = new BlockSparseMatrix(bs);
269 matrix->SetZero();
270
271 // Fill the values array of the block sparse matrix.
272 double* values = matrix->mutable_values();
273 for (int i = 0; i < column_blocks.size(); ++i) {
274 const int size = column_blocks[i].size;
275 for (int j = 0; j < size; ++j) {
276 // (j + 1) * size is compact way of accessing the (j,j) entry.
277 values[j * (size + 1)] = diagonal[j];
278 }
279 diagonal += size;
280 values += size * size;
281 }
282
283 return matrix;
284}
285
286void BlockSparseMatrix::AppendRows(const BlockSparseMatrix& m) {
287 CHECK_EQ(m.num_cols(), num_cols());
288 const CompressedRowBlockStructure* m_bs = m.block_structure();
289 CHECK_EQ(m_bs->cols.size(), block_structure_->cols.size());
290
291 const int old_num_nonzeros = num_nonzeros_;
292 const int old_num_row_blocks = block_structure_->rows.size();
293 block_structure_->rows.resize(old_num_row_blocks + m_bs->rows.size());
294
295 for (int i = 0; i < m_bs->rows.size(); ++i) {
296 const CompressedRow& m_row = m_bs->rows[i];
297 CompressedRow& row = block_structure_->rows[old_num_row_blocks + i];
298 row.block.size = m_row.block.size;
299 row.block.position = num_rows_;
300 num_rows_ += m_row.block.size;
301 row.cells.resize(m_row.cells.size());
302 for (int c = 0; c < m_row.cells.size(); ++c) {
303 const int block_id = m_row.cells[c].block_id;
304 row.cells[c].block_id = block_id;
305 row.cells[c].position = num_nonzeros_;
306 num_nonzeros_ += m_row.block.size * m_bs->cols[block_id].size;
307 }
308 }
309
310 if (num_nonzeros_ > max_num_nonzeros_) {
311 double* new_values = new double[num_nonzeros_];
312 std::copy(values_.get(), values_.get() + old_num_nonzeros, new_values);
313 values_.reset(new_values);
314 max_num_nonzeros_ = num_nonzeros_;
315 }
316
317 std::copy(m.values(),
318 m.values() + m.num_nonzeros(),
319 values_.get() + old_num_nonzeros);
320}
321
322void BlockSparseMatrix::DeleteRowBlocks(const int delta_row_blocks) {
323 const int num_row_blocks = block_structure_->rows.size();
324 int delta_num_nonzeros = 0;
325 int delta_num_rows = 0;
326 const std::vector<Block>& column_blocks = block_structure_->cols;
327 for (int i = 0; i < delta_row_blocks; ++i) {
328 const CompressedRow& row = block_structure_->rows[num_row_blocks - i - 1];
329 delta_num_rows += row.block.size;
330 for (int c = 0; c < row.cells.size(); ++c) {
331 const Cell& cell = row.cells[c];
332 delta_num_nonzeros += row.block.size * column_blocks[cell.block_id].size;
333 }
334 }
335 num_nonzeros_ -= delta_num_nonzeros;
336 num_rows_ -= delta_num_rows;
337 block_structure_->rows.resize(num_row_blocks - delta_row_blocks);
338}
339
340BlockSparseMatrix* BlockSparseMatrix::CreateRandomMatrix(
341 const BlockSparseMatrix::RandomMatrixOptions& options) {
342 CHECK_GT(options.num_row_blocks, 0);
343 CHECK_GT(options.min_row_block_size, 0);
344 CHECK_GT(options.max_row_block_size, 0);
345 CHECK_LE(options.min_row_block_size, options.max_row_block_size);
346 CHECK_GT(options.block_density, 0.0);
347 CHECK_LE(options.block_density, 1.0);
348
349 CompressedRowBlockStructure* bs = new CompressedRowBlockStructure();
350 if (options.col_blocks.empty()) {
351 CHECK_GT(options.num_col_blocks, 0);
352 CHECK_GT(options.min_col_block_size, 0);
353 CHECK_GT(options.max_col_block_size, 0);
354 CHECK_LE(options.min_col_block_size, options.max_col_block_size);
355
356 // Generate the col block structure.
357 int col_block_position = 0;
358 for (int i = 0; i < options.num_col_blocks; ++i) {
359 // Generate a random integer in [min_col_block_size, max_col_block_size]
360 const int delta_block_size =
361 Uniform(options.max_col_block_size - options.min_col_block_size);
362 const int col_block_size = options.min_col_block_size + delta_block_size;
363 bs->cols.push_back(Block(col_block_size, col_block_position));
364 col_block_position += col_block_size;
365 }
366 } else {
367 bs->cols = options.col_blocks;
368 }
369
370 bool matrix_has_blocks = false;
371 while (!matrix_has_blocks) {
372 VLOG(1) << "Clearing";
373 bs->rows.clear();
374 int row_block_position = 0;
375 int value_position = 0;
376 for (int r = 0; r < options.num_row_blocks; ++r) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800377 const int delta_block_size =
378 Uniform(options.max_row_block_size - options.min_row_block_size);
379 const int row_block_size = options.min_row_block_size + delta_block_size;
380 bs->rows.push_back(CompressedRow());
381 CompressedRow& row = bs->rows.back();
382 row.block.size = row_block_size;
383 row.block.position = row_block_position;
384 row_block_position += row_block_size;
385 for (int c = 0; c < bs->cols.size(); ++c) {
386 if (RandDouble() > options.block_density) continue;
387
388 row.cells.push_back(Cell());
389 Cell& cell = row.cells.back();
390 cell.block_id = c;
391 cell.position = value_position;
392 value_position += row_block_size * bs->cols[c].size;
393 matrix_has_blocks = true;
394 }
395 }
396 }
397
398 BlockSparseMatrix* matrix = new BlockSparseMatrix(bs);
399 double* values = matrix->mutable_values();
400 for (int i = 0; i < matrix->num_nonzeros(); ++i) {
401 values[i] = RandNormal();
402 }
403
404 return matrix;
405}
406
407} // namespace internal
408} // namespace ceres