1
// Ceres Solver - A fast non-linear least squares minimizer
2
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3
// http://code.google.com/p/ceres-solver/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
#include "ceres/block_random_access_sparse_matrix.h"
37
#include <glog/logging.h>
38
#include "ceres/mutex.h"
39
#include "ceres/triplet_sparse_matrix.h"
40
#include "ceres/internal/port.h"
41
#include "ceres/internal/scoped_ptr.h"
42
#include "ceres/types.h"
47
BlockRandomAccessSparseMatrix::BlockRandomAccessSparseMatrix(
48
const vector<int>& blocks,
49
const set<pair<int, int> >& block_pairs)
50
: kMaxRowBlocks(10 * 1000 * 1000),
52
CHECK_LT(blocks.size(), kMaxRowBlocks);
54
// Build the row/column layout vector and count the number of scalar
57
vector<int> col_layout;
58
for (int i = 0; i < blocks_.size(); ++i) {
59
col_layout.push_back(num_cols);
60
num_cols += blocks_[i];
63
// Count the number of scalar non-zero entries and build the layout
64
// object for looking into the values array of the
65
// TripletSparseMatrix.
67
for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
68
it != block_pairs.end();
70
const int row_block_size = blocks_[it->first];
71
const int col_block_size = blocks_[it->second];
72
num_nonzeros += row_block_size * col_block_size;
75
VLOG(1) << "Matrix Size [" << num_cols
77
<< "] " << num_nonzeros;
79
tsm_.reset(new TripletSparseMatrix(num_cols, num_cols, num_nonzeros));
80
tsm_->set_num_nonzeros(num_nonzeros);
81
int* rows = tsm_->mutable_rows();
82
int* cols = tsm_->mutable_cols();
83
double* values = tsm_->mutable_values();
86
for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
87
it != block_pairs.end();
89
const int row_block_size = blocks_[it->first];
90
const int col_block_size = blocks_[it->second];
91
layout_[IntPairToLong(it->first, it->second)] =
92
new CellInfo(values + pos);
93
pos += row_block_size * col_block_size;
96
// Fill the sparsity pattern of the underlying matrix.
97
for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
98
it != block_pairs.end();
100
const int row_block_id = it->first;
101
const int col_block_id = it->second;
102
const int row_block_size = blocks_[row_block_id];
103
const int col_block_size = blocks_[col_block_id];
105
layout_[IntPairToLong(row_block_id, col_block_id)]->values - values;
106
for (int r = 0; r < row_block_size; ++r) {
107
for (int c = 0; c < col_block_size; ++c, ++pos) {
108
rows[pos] = col_layout[row_block_id] + r;
109
cols[pos] = col_layout[col_block_id] + c;
111
DCHECK_LT(rows[pos], tsm_->num_rows());
112
DCHECK_LT(cols[pos], tsm_->num_rows());
118
// Assume that the user does not hold any locks on any cell blocks
119
// when they are calling SetZero.
120
BlockRandomAccessSparseMatrix::~BlockRandomAccessSparseMatrix() {
121
for (LayoutType::iterator it = layout_.begin();
128
CellInfo* BlockRandomAccessSparseMatrix::GetCell(int row_block_id,
134
const LayoutType::iterator it =
135
layout_.find(IntPairToLong(row_block_id, col_block_id));
136
if (it == layout_.end()) {
140
// Each cell is stored contiguously as its own little dense matrix.
143
*row_stride = blocks_[row_block_id];
144
*col_stride = blocks_[col_block_id];
148
// Assume that the user does not hold any locks on any cell blocks
149
// when they are calling SetZero.
150
void BlockRandomAccessSparseMatrix::SetZero() {
151
if (tsm_->num_nonzeros()) {
152
VectorRef(tsm_->mutable_values(),
153
tsm_->num_nonzeros()).setZero();
157
} // namespace internal