blob: 7ee419e72acd06588de68c13138f974a6be3dd9b [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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// TODO(sameeragarwal): row_block_counter can perhaps be replaced by
32// Chunk::start ?
33
34#ifndef CERES_INTERNAL_SCHUR_ELIMINATOR_IMPL_H_
35#define CERES_INTERNAL_SCHUR_ELIMINATOR_IMPL_H_
36
Keir Mierle8ebb0732012-04-30 23:09:08 -070037// Eigen has an internal threshold switching between different matrix
38// multiplication algorithms. In particular for matrices larger than
39// EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD it uses a cache friendly
40// matrix matrix product algorithm that has a higher setup cost. For
41// matrix sizes close to this threshold, especially when the matrices
42// are thin and long, the default choice may not be optimal. This is
43// the case for us, as the default choice causes a 30% performance
44// regression when we moved from Eigen2 to Eigen3.
Sameer Agarwal296fa9b2013-04-02 09:44:15 -070045
Keir Mierle8ebb0732012-04-30 23:09:08 -070046#define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 10
47
Alex Stewartea765852014-05-07 20:46:17 +010048// This include must come before any #ifndef check on Ceres compile options.
49#include "ceres/internal/port.h"
50
Sameer Agarwal296fa9b2013-04-02 09:44:15 -070051#ifdef CERES_USE_OPENMP
52#include <omp.h>
53#endif
54
Keir Mierle8ebb0732012-04-30 23:09:08 -070055#include <algorithm>
56#include <map>
Keir Mierle8ebb0732012-04-30 23:09:08 -070057#include "ceres/block_random_access_matrix.h"
58#include "ceres/block_sparse_matrix.h"
59#include "ceres/block_structure.h"
Sameer Agarwal296fa9b2013-04-02 09:44:15 -070060#include "ceres/internal/eigen.h"
Sameer Agarwal487250e2013-04-05 14:20:37 -070061#include "ceres/internal/fixed_array.h"
Sameer Agarwal296fa9b2013-04-02 09:44:15 -070062#include "ceres/internal/scoped_ptr.h"
Sameer Agarwal0859fe82017-04-09 00:45:12 -070063#include "ceres/invert_psd_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070064#include "ceres/map_util.h"
65#include "ceres/schur_eliminator.h"
Sameer Agarwal367b65e2013-08-09 10:35:37 -070066#include "ceres/small_blas.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070067#include "ceres/stl_util.h"
Sameer Agarwal487250e2013-04-05 14:20:37 -070068#include "Eigen/Dense"
Sameer Agarwal509f68c2013-02-20 01:39:03 -080069#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070070
71namespace ceres {
72namespace internal {
73
74template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
75SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::~SchurEliminator() {
76 STLDeleteElements(&rhs_locks_);
77}
78
79template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
Sameer Agarwal0859fe82017-04-09 00:45:12 -070080void SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::Init(
81 int num_eliminate_blocks,
82 bool assume_full_rank_ete,
83 const CompressedRowBlockStructure* bs) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070084 CHECK_GT(num_eliminate_blocks, 0)
85 << "SchurComplementSolver cannot be initialized with "
86 << "num_eliminate_blocks = 0.";
87
88 num_eliminate_blocks_ = num_eliminate_blocks;
Sameer Agarwal0859fe82017-04-09 00:45:12 -070089 assume_full_rank_ete_ = assume_full_rank_ete;
Keir Mierle8ebb0732012-04-30 23:09:08 -070090
91 const int num_col_blocks = bs->cols.size();
92 const int num_row_blocks = bs->rows.size();
93
94 buffer_size_ = 1;
95 chunks_.clear();
96 lhs_row_layout_.clear();
97
98 int lhs_num_rows = 0;
99 // Add a map object for each block in the reduced linear system
100 // and build the row/column block structure of the reduced linear
101 // system.
102 lhs_row_layout_.resize(num_col_blocks - num_eliminate_blocks_);
103 for (int i = num_eliminate_blocks_; i < num_col_blocks; ++i) {
104 lhs_row_layout_[i - num_eliminate_blocks_] = lhs_num_rows;
105 lhs_num_rows += bs->cols[i].size;
106 }
107
108 int r = 0;
109 // Iterate over the row blocks of A, and detect the chunks. The
110 // matrix should already have been ordered so that all rows
111 // containing the same y block are vertically contiguous. Along
112 // the way also compute the amount of space each chunk will need
113 // to perform the elimination.
114 while (r < num_row_blocks) {
115 const int chunk_block_id = bs->rows[r].cells.front().block_id;
116 if (chunk_block_id >= num_eliminate_blocks_) {
117 break;
118 }
119
120 chunks_.push_back(Chunk());
121 Chunk& chunk = chunks_.back();
122 chunk.size = 0;
123 chunk.start = r;
124 int buffer_size = 0;
125 const int e_block_size = bs->cols[chunk_block_id].size;
126
127 // Add to the chunk until the first block in the row is
128 // different than the one in the first row for the chunk.
129 while (r + chunk.size < num_row_blocks) {
130 const CompressedRow& row = bs->rows[r + chunk.size];
131 if (row.cells.front().block_id != chunk_block_id) {
132 break;
133 }
134
135 // Iterate over the blocks in the row, ignoring the first
136 // block since it is the one to be eliminated.
137 for (int c = 1; c < row.cells.size(); ++c) {
138 const Cell& cell = row.cells[c];
139 if (InsertIfNotPresent(
140 &(chunk.buffer_layout), cell.block_id, buffer_size)) {
141 buffer_size += e_block_size * bs->cols[cell.block_id].size;
142 }
143 }
144
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800145 buffer_size_ = std::max(buffer_size, buffer_size_);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700146 ++chunk.size;
147 }
148
149 CHECK_GT(chunk.size, 0);
150 r += chunk.size;
151 }
152 const Chunk& chunk = chunks_.back();
153
154 uneliminated_row_begins_ = chunk.start + chunk.size;
155 if (num_threads_ > 1) {
156 random_shuffle(chunks_.begin(), chunks_.end());
157 }
158
159 buffer_.reset(new double[buffer_size_ * num_threads_]);
160
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700161 // chunk_outer_product_buffer_ only needs to store e_block_size *
162 // f_block_size, which is always less than buffer_size_, so we just
163 // allocate buffer_size_ per thread.
164 chunk_outer_product_buffer_.reset(new double[buffer_size_ * num_threads_]);
165
Keir Mierle8ebb0732012-04-30 23:09:08 -0700166 STLDeleteElements(&rhs_locks_);
167 rhs_locks_.resize(num_col_blocks - num_eliminate_blocks_);
168 for (int i = 0; i < num_col_blocks - num_eliminate_blocks_; ++i) {
169 rhs_locks_[i] = new Mutex;
170 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700171}
172
173template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
174void
175SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700176Eliminate(const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700177 const double* b,
178 const double* D,
179 BlockRandomAccessMatrix* lhs,
180 double* rhs) {
181 if (lhs->num_rows() > 0) {
182 lhs->SetZero();
183 VectorRef(rhs, lhs->num_rows()).setZero();
184 }
185
186 const CompressedRowBlockStructure* bs = A->block_structure();
187 const int num_col_blocks = bs->cols.size();
188
189 // Add the diagonal to the schur complement.
190 if (D != NULL) {
191#pragma omp parallel for num_threads(num_threads_) schedule(dynamic)
192 for (int i = num_eliminate_blocks_; i < num_col_blocks; ++i) {
193 const int block_id = i - num_eliminate_blocks_;
194 int r, c, row_stride, col_stride;
195 CellInfo* cell_info = lhs->GetCell(block_id, block_id,
196 &r, &c,
197 &row_stride, &col_stride);
198 if (cell_info != NULL) {
199 const int block_size = bs->cols[i].size;
Sameer Agarwal4bf38682015-08-09 15:24:45 -0700200 typename EigenTypes<Eigen::Dynamic>::ConstVectorRef
Keir Mierle8ebb0732012-04-30 23:09:08 -0700201 diag(D + bs->cols[i].position, block_size);
202
Keir Mierleff71d742012-08-10 17:05:15 -0700203 CeresMutexLock l(&cell_info->m);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700204 MatrixRef m(cell_info->values, row_stride, col_stride);
205 m.block(r, c, block_size, block_size).diagonal()
206 += diag.array().square().matrix();
207 }
208 }
209 }
210
Sameer Agarwal4bf38682015-08-09 15:24:45 -0700211 // Eliminate y blocks one chunk at a time. For each chunk, compute
212 // the entries of the normal equations and the gradient vector block
213 // corresponding to the y block and then apply Gaussian elimination
214 // to them. The matrix ete stores the normal matrix corresponding to
215 // the block being eliminated and array buffer_ contains the
216 // non-zero blocks in the row corresponding to this y block in the
217 // normal equations. This computation is done in
218 // ChunkDiagonalBlockAndGradient. UpdateRhs then applies gaussian
219 // elimination to the rhs of the normal equations, updating the rhs
220 // of the reduced linear system by modifying rhs blocks for all the
221 // z blocks that share a row block/residual term with the y
222 // block. EliminateRowOuterProduct does the corresponding operation
223 // for the lhs of the reduced linear system.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700224#pragma omp parallel for num_threads(num_threads_) schedule(dynamic)
225 for (int i = 0; i < chunks_.size(); ++i) {
226#ifdef CERES_USE_OPENMP
227 int thread_id = omp_get_thread_num();
228#else
229 int thread_id = 0;
230#endif
231 double* buffer = buffer_.get() + thread_id * buffer_size_;
232 const Chunk& chunk = chunks_[i];
233 const int e_block_id = bs->rows[chunk.start].cells.front().block_id;
234 const int e_block_size = bs->cols[e_block_id].size;
235
236 VectorRef(buffer, buffer_size_).setZero();
237
238 typename EigenTypes<kEBlockSize, kEBlockSize>::Matrix
239 ete(e_block_size, e_block_size);
240
241 if (D != NULL) {
242 const typename EigenTypes<kEBlockSize>::ConstVectorRef
243 diag(D + bs->cols[e_block_id].position, e_block_size);
244 ete = diag.array().square().matrix().asDiagonal();
245 } else {
246 ete.setZero();
247 }
248
Sameer Agarwal487250e2013-04-05 14:20:37 -0700249 FixedArray<double, 8> g(e_block_size);
250 typename EigenTypes<kEBlockSize>::VectorRef gref(g.get(), e_block_size);
251 gref.setZero();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700252
253 // We are going to be computing
254 //
255 // S += F'F - F'E(E'E)^{-1}E'F
256 //
257 // for each Chunk. The computation is broken down into a number of
258 // function calls as below.
259
260 // Compute the outer product of the e_blocks with themselves (ete
261 // = E'E). Compute the product of the e_blocks with the
262 // corresonding f_blocks (buffer = E'F), the gradient of the terms
263 // in this chunk (g) and add the outer product of the f_blocks to
264 // Schur complement (S += F'F).
265 ChunkDiagonalBlockAndGradient(
Sameer Agarwal487250e2013-04-05 14:20:37 -0700266 chunk, A, b, chunk.start, &ete, g.get(), buffer, lhs);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700267
268 // Normally one wouldn't compute the inverse explicitly, but
269 // e_block_size will typically be a small number like 3, in
270 // which case its much faster to compute the inverse once and
271 // use it to multiply other matrices/vectors instead of doing a
272 // Solve call over and over again.
Sameer Agarwale712ce12015-04-07 14:11:10 -0700273 typename EigenTypes<kEBlockSize, kEBlockSize>::Matrix inverse_ete =
Sameer Agarwal0859fe82017-04-09 00:45:12 -0700274 InvertPSDMatrix<kEBlockSize>(assume_full_rank_ete_, ete);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700275
276 // For the current chunk compute and update the rhs of the reduced
277 // linear system.
278 //
279 // rhs = F'b - F'E(E'E)^(-1) E'b
Sameer Agarwal487250e2013-04-05 14:20:37 -0700280
281 FixedArray<double, 8> inverse_ete_g(e_block_size);
282 MatrixVectorMultiply<kEBlockSize, kEBlockSize, 0>(
Sameer Agarwale6707b22013-04-16 15:44:23 -0700283 inverse_ete.data(),
284 e_block_size,
285 e_block_size,
286 g.get(),
287 inverse_ete_g.get());
Sameer Agarwal487250e2013-04-05 14:20:37 -0700288
289 UpdateRhs(chunk, A, b, chunk.start, inverse_ete_g.get(), rhs);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700290
291 // S -= F'E(E'E)^{-1}E'F
292 ChunkOuterProduct(bs, inverse_ete, buffer, chunk.buffer_layout, lhs);
293 }
294
295 // For rows with no e_blocks, the schur complement update reduces to
296 // S += F'F.
297 NoEBlockRowsUpdate(A, b, uneliminated_row_begins_, lhs, rhs);
298}
299
300template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
301void
302SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700303BackSubstitute(const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700304 const double* b,
305 const double* D,
306 const double* z,
307 double* y) {
308 const CompressedRowBlockStructure* bs = A->block_structure();
309#pragma omp parallel for num_threads(num_threads_) schedule(dynamic)
310 for (int i = 0; i < chunks_.size(); ++i) {
311 const Chunk& chunk = chunks_[i];
312 const int e_block_id = bs->rows[chunk.start].cells.front().block_id;
313 const int e_block_size = bs->cols[e_block_id].size;
314
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700315 double* y_ptr = y + bs->cols[e_block_id].position;
316 typename EigenTypes<kEBlockSize>::VectorRef y_block(y_ptr, e_block_size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700317
318 typename EigenTypes<kEBlockSize, kEBlockSize>::Matrix
319 ete(e_block_size, e_block_size);
320 if (D != NULL) {
321 const typename EigenTypes<kEBlockSize>::ConstVectorRef
322 diag(D + bs->cols[e_block_id].position, e_block_size);
323 ete = diag.array().square().matrix().asDiagonal();
324 } else {
325 ete.setZero();
326 }
327
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700328 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700329 for (int j = 0; j < chunk.size; ++j) {
330 const CompressedRow& row = bs->rows[chunk.start + j];
Keir Mierle8ebb0732012-04-30 23:09:08 -0700331 const Cell& e_cell = row.cells.front();
332 DCHECK_EQ(e_block_id, e_cell.block_id);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700333
Sameer Agarwal487250e2013-04-05 14:20:37 -0700334 FixedArray<double, 8> sj(row.block.size);
335
336 typename EigenTypes<kRowBlockSize>::VectorRef(sj.get(), row.block.size) =
Keir Mierle8ebb0732012-04-30 23:09:08 -0700337 typename EigenTypes<kRowBlockSize>::ConstVectorRef
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700338 (b + bs->rows[chunk.start + j].block.position, row.block.size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700339
340 for (int c = 1; c < row.cells.size(); ++c) {
341 const int f_block_id = row.cells[c].block_id;
342 const int f_block_size = bs->cols[f_block_id].size;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700343 const int r_block = f_block_id - num_eliminate_blocks_;
344
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700345 MatrixVectorMultiply<kRowBlockSize, kFBlockSize, -1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700346 values + row.cells[c].position, row.block.size, f_block_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700347 z + lhs_row_layout_[r_block],
Sameer Agarwal487250e2013-04-05 14:20:37 -0700348 sj.get());
Keir Mierle8ebb0732012-04-30 23:09:08 -0700349 }
350
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700351 MatrixTransposeVectorMultiply<kRowBlockSize, kEBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700352 values + e_cell.position, row.block.size, e_block_size,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700353 sj.get(),
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700354 y_ptr);
355
356 MatrixTransposeMatrixMultiply
Sameer Agarwal487250e2013-04-05 14:20:37 -0700357 <kRowBlockSize, kEBlockSize, kRowBlockSize, kEBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700358 values + e_cell.position, row.block.size, e_block_size,
359 values + e_cell.position, row.block.size, e_block_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700360 ete.data(), 0, 0, e_block_size, e_block_size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700361 }
362
Sameer Agarwal0859fe82017-04-09 00:45:12 -0700363 y_block = InvertPSDMatrix<kEBlockSize>(assume_full_rank_ete_, ete)
364 * y_block;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700365 }
366}
367
368// Update the rhs of the reduced linear system. Compute
369//
370// F'b - F'E(E'E)^(-1) E'b
371template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
372void
373SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
374UpdateRhs(const Chunk& chunk,
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700375 const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700376 const double* b,
377 int row_block_counter,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700378 const double* inverse_ete_g,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700379 double* rhs) {
380 const CompressedRowBlockStructure* bs = A->block_structure();
Sameer Agarwal487250e2013-04-05 14:20:37 -0700381 const int e_block_id = bs->rows[chunk.start].cells.front().block_id;
382 const int e_block_size = bs->cols[e_block_id].size;
383
Keir Mierle8ebb0732012-04-30 23:09:08 -0700384 int b_pos = bs->rows[row_block_counter].block.position;
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700385 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700386 for (int j = 0; j < chunk.size; ++j) {
387 const CompressedRow& row = bs->rows[row_block_counter + j];
Keir Mierle8ebb0732012-04-30 23:09:08 -0700388 const Cell& e_cell = row.cells.front();
389
Sameer Agarwal487250e2013-04-05 14:20:37 -0700390 typename EigenTypes<kRowBlockSize>::Vector sj =
Keir Mierle8ebb0732012-04-30 23:09:08 -0700391 typename EigenTypes<kRowBlockSize>::ConstVectorRef
Sameer Agarwal487250e2013-04-05 14:20:37 -0700392 (b + b_pos, row.block.size);
393
394 MatrixVectorMultiply<kRowBlockSize, kEBlockSize, -1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700395 values + e_cell.position, row.block.size, e_block_size,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700396 inverse_ete_g, sj.data());
Keir Mierle8ebb0732012-04-30 23:09:08 -0700397
398 for (int c = 1; c < row.cells.size(); ++c) {
399 const int block_id = row.cells[c].block_id;
400 const int block_size = bs->cols[block_id].size;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700401 const int block = block_id - num_eliminate_blocks_;
Keir Mierleff71d742012-08-10 17:05:15 -0700402 CeresMutexLock l(rhs_locks_[block]);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700403 MatrixTransposeVectorMultiply<kRowBlockSize, kFBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700404 values + row.cells[c].position,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700405 row.block.size, block_size,
406 sj.data(), rhs + lhs_row_layout_[block]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700407 }
408 b_pos += row.block.size;
409 }
410}
411
412// Given a Chunk - set of rows with the same e_block, e.g. in the
413// following Chunk with two rows.
414//
415// E F
416// [ y11 0 0 0 | z11 0 0 0 z51]
417// [ y12 0 0 0 | z12 z22 0 0 0]
418//
419// this function computes twp matrices. The diagonal block matrix
420//
421// ete = y11 * y11' + y12 * y12'
422//
423// and the off diagonal blocks in the Guass Newton Hessian.
424//
425// buffer = [y11'(z11 + z12), y12' * z22, y11' * z51]
426//
427// which are zero compressed versions of the block sparse matrices E'E
428// and E'F.
429//
430// and the gradient of the e_block, E'b.
431template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
432void
433SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
434ChunkDiagonalBlockAndGradient(
435 const Chunk& chunk,
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700436 const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700437 const double* b,
438 int row_block_counter,
439 typename EigenTypes<kEBlockSize, kEBlockSize>::Matrix* ete,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700440 double* g,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700441 double* buffer,
442 BlockRandomAccessMatrix* lhs) {
443 const CompressedRowBlockStructure* bs = A->block_structure();
444
445 int b_pos = bs->rows[row_block_counter].block.position;
446 const int e_block_size = ete->rows();
447
448 // Iterate over the rows in this chunk, for each row, compute the
449 // contribution of its F blocks to the Schur complement, the
450 // contribution of its E block to the matrix EE' (ete), and the
451 // corresponding block in the gradient vector.
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700452 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700453 for (int j = 0; j < chunk.size; ++j) {
454 const CompressedRow& row = bs->rows[row_block_counter + j];
Keir Mierle8ebb0732012-04-30 23:09:08 -0700455
456 if (row.cells.size() > 1) {
457 EBlockRowOuterProduct(A, row_block_counter + j, lhs);
458 }
459
460 // Extract the e_block, ETE += E_i' E_i
461 const Cell& e_cell = row.cells.front();
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700462 MatrixTransposeMatrixMultiply
463 <kRowBlockSize, kEBlockSize, kRowBlockSize, kEBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700464 values + e_cell.position, row.block.size, e_block_size,
465 values + e_cell.position, row.block.size, e_block_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700466 ete->data(), 0, 0, e_block_size, e_block_size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700467
468 // g += E_i' b_i
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700469 MatrixTransposeVectorMultiply<kRowBlockSize, kEBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700470 values + e_cell.position, row.block.size, e_block_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700471 b + b_pos,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700472 g);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700473
Keir Mierle8ebb0732012-04-30 23:09:08 -0700474
475 // buffer = E'F. This computation is done by iterating over the
476 // f_blocks for each row in the chunk.
477 for (int c = 1; c < row.cells.size(); ++c) {
478 const int f_block_id = row.cells[c].block_id;
479 const int f_block_size = bs->cols[f_block_id].size;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700480 double* buffer_ptr =
481 buffer + FindOrDie(chunk.buffer_layout, f_block_id);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700482 MatrixTransposeMatrixMultiply
483 <kRowBlockSize, kEBlockSize, kRowBlockSize, kFBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700484 values + e_cell.position, row.block.size, e_block_size,
485 values + row.cells[c].position, row.block.size, f_block_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700486 buffer_ptr, 0, 0, e_block_size, f_block_size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700487 }
488 b_pos += row.block.size;
489 }
490}
491
492// Compute the outer product F'E(E'E)^{-1}E'F and subtract it from the
493// Schur complement matrix, i.e
494//
495// S -= F'E(E'E)^{-1}E'F.
496template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
497void
498SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
499ChunkOuterProduct(const CompressedRowBlockStructure* bs,
500 const Matrix& inverse_ete,
501 const double* buffer,
502 const BufferLayoutType& buffer_layout,
503 BlockRandomAccessMatrix* lhs) {
504 // This is the most computationally expensive part of this
505 // code. Profiling experiments reveal that the bottleneck is not the
506 // computation of the right-hand matrix product, but memory
507 // references to the left hand side.
508 const int e_block_size = inverse_ete.rows();
509 BufferLayoutType::const_iterator it1 = buffer_layout.begin();
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700510
511#ifdef CERES_USE_OPENMP
512 int thread_id = omp_get_thread_num();
513#else
514 int thread_id = 0;
515#endif
516 double* b1_transpose_inverse_ete =
517 chunk_outer_product_buffer_.get() + thread_id * buffer_size_;
518
Keir Mierle8ebb0732012-04-30 23:09:08 -0700519 // S(i,j) -= bi' * ete^{-1} b_j
520 for (; it1 != buffer_layout.end(); ++it1) {
521 const int block1 = it1->first - num_eliminate_blocks_;
522 const int block1_size = bs->cols[it1->first].size;
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700523 MatrixTransposeMatrixMultiply
524 <kEBlockSize, kFBlockSize, kEBlockSize, kEBlockSize, 0>(
525 buffer + it1->second, e_block_size, block1_size,
526 inverse_ete.data(), e_block_size, e_block_size,
527 b1_transpose_inverse_ete, 0, 0, block1_size, e_block_size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700528
529 BufferLayoutType::const_iterator it2 = it1;
530 for (; it2 != buffer_layout.end(); ++it2) {
531 const int block2 = it2->first - num_eliminate_blocks_;
532
533 int r, c, row_stride, col_stride;
534 CellInfo* cell_info = lhs->GetCell(block1, block2,
535 &r, &c,
536 &row_stride, &col_stride);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700537 if (cell_info != NULL) {
538 const int block2_size = bs->cols[it2->first].size;
539 CeresMutexLock l(&cell_info->m);
540 MatrixMatrixMultiply
541 <kFBlockSize, kEBlockSize, kEBlockSize, kFBlockSize, -1>(
542 b1_transpose_inverse_ete, block1_size, e_block_size,
543 buffer + it2->second, e_block_size, block2_size,
544 cell_info->values, r, c, row_stride, col_stride);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700545 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700546 }
547 }
548}
549
550// For rows with no e_blocks, the schur complement update reduces to S
551// += F'F. This function iterates over the rows of A with no e_block,
552// and calls NoEBlockRowOuterProduct on each row.
553template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
554void
555SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700556NoEBlockRowsUpdate(const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700557 const double* b,
558 int row_block_counter,
559 BlockRandomAccessMatrix* lhs,
560 double* rhs) {
561 const CompressedRowBlockStructure* bs = A->block_structure();
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700562 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700563 for (; row_block_counter < bs->rows.size(); ++row_block_counter) {
564 const CompressedRow& row = bs->rows[row_block_counter];
Keir Mierle8ebb0732012-04-30 23:09:08 -0700565 for (int c = 0; c < row.cells.size(); ++c) {
566 const int block_id = row.cells[c].block_id;
567 const int block_size = bs->cols[block_id].size;
568 const int block = block_id - num_eliminate_blocks_;
Sameer Agarwal487250e2013-04-05 14:20:37 -0700569 MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700570 values + row.cells[c].position, row.block.size, block_size,
Sameer Agarwal487250e2013-04-05 14:20:37 -0700571 b + row.block.position,
572 rhs + lhs_row_layout_[block]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700573 }
574 NoEBlockRowOuterProduct(A, row_block_counter, lhs);
575 }
576}
577
578
579// A row r of A, which has no e_blocks gets added to the Schur
580// Complement as S += r r'. This function is responsible for computing
581// the contribution of a single row r to the Schur complement. It is
582// very similar in structure to EBlockRowOuterProduct except for
583// one difference. It does not use any of the template
584// parameters. This is because the algorithm used for detecting the
585// static structure of the matrix A only pays attention to rows with
586// e_blocks. This is becase rows without e_blocks are rare and
587// typically arise from regularization terms in the original
588// optimization problem, and have a very different structure than the
589// rows with e_blocks. Including them in the static structure
590// detection will lead to most template parameters being set to
591// dynamic. Since the number of rows without e_blocks is small, the
592// lack of templating is not an issue.
593template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
594void
595SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700596NoEBlockRowOuterProduct(const BlockSparseMatrix* A,
Sameer Agarwal4bf38682015-08-09 15:24:45 -0700597 int row_block_index,
598 BlockRandomAccessMatrix* lhs) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700599 const CompressedRowBlockStructure* bs = A->block_structure();
600 const CompressedRow& row = bs->rows[row_block_index];
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700601 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700602 for (int i = 0; i < row.cells.size(); ++i) {
603 const int block1 = row.cells[i].block_id - num_eliminate_blocks_;
604 DCHECK_GE(block1, 0);
605
606 const int block1_size = bs->cols[row.cells[i].block_id].size;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700607 int r, c, row_stride, col_stride;
608 CellInfo* cell_info = lhs->GetCell(block1, block1,
609 &r, &c,
610 &row_stride, &col_stride);
611 if (cell_info != NULL) {
Keir Mierleff71d742012-08-10 17:05:15 -0700612 CeresMutexLock l(&cell_info->m);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700613 // This multiply currently ignores the fact that this is a
614 // symmetric outer product.
615 MatrixTransposeMatrixMultiply
616 <Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700617 values + row.cells[i].position, row.block.size, block1_size,
618 values + row.cells[i].position, row.block.size, block1_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700619 cell_info->values, r, c, row_stride, col_stride);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700620 }
621
622 for (int j = i + 1; j < row.cells.size(); ++j) {
623 const int block2 = row.cells[j].block_id - num_eliminate_blocks_;
624 DCHECK_GE(block2, 0);
625 DCHECK_LT(block1, block2);
626 int r, c, row_stride, col_stride;
627 CellInfo* cell_info = lhs->GetCell(block1, block2,
628 &r, &c,
629 &row_stride, &col_stride);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700630 if (cell_info != NULL) {
631 const int block2_size = bs->cols[row.cells[j].block_id].size;
632 CeresMutexLock l(&cell_info->m);
633 MatrixTransposeMatrixMultiply
634 <Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic, Eigen::Dynamic, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700635 values + row.cells[i].position, row.block.size, block1_size,
636 values + row.cells[j].position, row.block.size, block2_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700637 cell_info->values, r, c, row_stride, col_stride);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700638 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700639 }
640 }
641}
642
643// For a row with an e_block, compute the contribition S += F'F. This
644// function has the same structure as NoEBlockRowOuterProduct, except
645// that this function uses the template parameters.
646template <int kRowBlockSize, int kEBlockSize, int kFBlockSize>
647void
648SchurEliminator<kRowBlockSize, kEBlockSize, kFBlockSize>::
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700649EBlockRowOuterProduct(const BlockSparseMatrix* A,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700650 int row_block_index,
651 BlockRandomAccessMatrix* lhs) {
652 const CompressedRowBlockStructure* bs = A->block_structure();
653 const CompressedRow& row = bs->rows[row_block_index];
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700654 const double* values = A->values();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700655 for (int i = 1; i < row.cells.size(); ++i) {
656 const int block1 = row.cells[i].block_id - num_eliminate_blocks_;
657 DCHECK_GE(block1, 0);
658
659 const int block1_size = bs->cols[row.cells[i].block_id].size;
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700660 int r, c, row_stride, col_stride;
661 CellInfo* cell_info = lhs->GetCell(block1, block1,
662 &r, &c,
663 &row_stride, &col_stride);
664 if (cell_info != NULL) {
Keir Mierleff71d742012-08-10 17:05:15 -0700665 CeresMutexLock l(&cell_info->m);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700666 // block += b1.transpose() * b1;
667 MatrixTransposeMatrixMultiply
668 <kRowBlockSize, kFBlockSize, kRowBlockSize, kFBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700669 values + row.cells[i].position, row.block.size, block1_size,
670 values + row.cells[i].position, row.block.size, block1_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700671 cell_info->values, r, c, row_stride, col_stride);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700672 }
673
674 for (int j = i + 1; j < row.cells.size(); ++j) {
675 const int block2 = row.cells[j].block_id - num_eliminate_blocks_;
676 DCHECK_GE(block2, 0);
677 DCHECK_LT(block1, block2);
678 const int block2_size = bs->cols[row.cells[j].block_id].size;
679 int r, c, row_stride, col_stride;
680 CellInfo* cell_info = lhs->GetCell(block1, block2,
681 &r, &c,
682 &row_stride, &col_stride);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700683 if (cell_info != NULL) {
684 // block += b1.transpose() * b2;
Sameer Agarwalc7e69be2013-04-16 09:39:16 -0700685 CeresMutexLock l(&cell_info->m);
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700686 MatrixTransposeMatrixMultiply
687 <kRowBlockSize, kFBlockSize, kRowBlockSize, kFBlockSize, 1>(
Sameer Agarwalc1e10d92013-04-24 11:58:24 -0700688 values + row.cells[i].position, row.block.size, block1_size,
689 values + row.cells[j].position, row.block.size, block2_size,
Sameer Agarwal296fa9b2013-04-02 09:44:15 -0700690 cell_info->values, r, c, row_stride, col_stride);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700691 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700692 }
693 }
694}
695
696} // namespace internal
697} // namespace ceres
698
699#endif // CERES_INTERNAL_SCHUR_ELIMINATOR_IMPL_H_