mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-31 01:20:38 +08:00
739f2a25ae
Use ParallelFor to parallelize both versions of the block Jacobi preconditioner. Also add benchmarks for varying number of threads. Benchmark on M1 Mac Pro Before: ----------------------------------------------------------------------------------------- Benchmark Time CPU Iterations ----------------------------------------------------------------------------------------- BM_BlockSparseJacobiPreconditionerBA 44847927 ns 44788313 ns 16 BM_BlockCRSJacobiPreconditionerBA 48772330 ns 48723571 ns 14 BM_BlockSparseJacobiPreconditionerUnstructured 62385231 ns 62306818 ns 11 BM_BlockCRSJacobiPreconditionerUnstructured 60671473 ns 60577727 ns 11 After: -------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------------------------------------- BM_BlockSparseJacobiPreconditionerBA/1 53314862 ns 53302308 ns 13 BM_BlockSparseJacobiPreconditionerBA/2 33601214 ns 33295143 ns 21 BM_BlockSparseJacobiPreconditionerBA/4 28162794 ns 27224167 ns 30 BM_BlockSparseJacobiPreconditionerBA/8 31402448 ns 28038760 ns 25 BM_BlockSparseJacobiPreconditionerBA/16 30820813 ns 22625233 ns 30 BM_BlockCRSJacobiPreconditionerBA/1 60348194 ns 60332167 ns 12 BM_BlockCRSJacobiPreconditionerBA/2 35489954 ns 34782050 ns 20 BM_BlockCRSJacobiPreconditionerBA/4 23636360 ns 22547032 ns 31 BM_BlockCRSJacobiPreconditionerBA/8 31688798 ns 27857800 ns 25 BM_BlockCRSJacobiPreconditionerBA/16 30806695 ns 20562516 ns 31 BM_BlockSparseJacobiPreconditionerUnstructured/1 59793396 ns 59788583 ns 12 BM_BlockSparseJacobiPreconditionerUnstructured/2 35192900 ns 34968900 ns 20 BM_BlockSparseJacobiPreconditionerUnstructured/4 30171145 ns 28924480 ns 25 BM_BlockSparseJacobiPreconditionerUnstructured/8 24982583 ns 23193172 ns 29 BM_BlockSparseJacobiPreconditionerUnstructured/16 23370546 ns 18389694 ns 36 BM_BlockCRSJacobiPreconditionerUnstructured/1 63204538 ns 63204545 ns 11 BM_BlockCRSJacobiPreconditionerUnstructured/2 34466060 ns 34193429 ns 21 BM_BlockCRSJacobiPreconditionerUnstructured/4 22712230 ns 20491147 ns 34 BM_BlockCRSJacobiPreconditionerUnstructured/8 16701833 ns 16190395 ns 43 BM_BlockCRSJacobiPreconditionerUnstructured/16 16762565 ns 12857304 ns 56 Note that single threaded performance gets worse. Performance goes up for 2 and 4 threads and then essentially stalls. Change-Id: I96a5d2f719545e14c03d73e71c8c0564e8c1c729
158 lines
6.1 KiB
C++
158 lines
6.1 KiB
C++
// Ceres Solver - A fast non-linear least squares minimizer
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
// http://ceres-solver.org/
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
// used to endorse or promote products derived from this software without
|
|
// specific prior written permission.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
//
|
|
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
|
|
#include "ceres/block_jacobi_preconditioner.h"
|
|
|
|
#include <memory>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
#include "Eigen/Dense"
|
|
#include "ceres/block_random_access_diagonal_matrix.h"
|
|
#include "ceres/block_sparse_matrix.h"
|
|
#include "ceres/linear_least_squares_problems.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace ceres::internal {
|
|
|
|
TEST(BlockSparseJacobiPreconditioner, _) {
|
|
constexpr int kNumtrials = 10;
|
|
BlockSparseMatrix::RandomMatrixOptions options;
|
|
options.num_col_blocks = 3;
|
|
options.min_col_block_size = 1;
|
|
options.max_col_block_size = 3;
|
|
|
|
options.num_row_blocks = 5;
|
|
options.min_row_block_size = 1;
|
|
options.max_row_block_size = 4;
|
|
options.block_density = 0.25;
|
|
std::mt19937 prng;
|
|
|
|
Preconditioner::Options preconditioner_options;
|
|
ContextImpl context;
|
|
preconditioner_options.context = &context;
|
|
|
|
for (int trial = 0; trial < kNumtrials; ++trial) {
|
|
auto jacobian = BlockSparseMatrix::CreateRandomMatrix(options, prng);
|
|
Vector diagonal = Vector::Ones(jacobian->num_cols());
|
|
Matrix dense_jacobian;
|
|
jacobian->ToDenseMatrix(&dense_jacobian);
|
|
Matrix hessian = dense_jacobian.transpose() * dense_jacobian;
|
|
hessian.diagonal() += diagonal.array().square().matrix();
|
|
|
|
BlockSparseJacobiPreconditioner pre(preconditioner_options, *jacobian);
|
|
pre.Update(*jacobian, diagonal.data());
|
|
|
|
// The const_cast is needed to be able to call GetCell.
|
|
auto* m = const_cast<BlockRandomAccessDiagonalMatrix*>(&pre.matrix());
|
|
EXPECT_EQ(m->num_rows(), jacobian->num_cols());
|
|
EXPECT_EQ(m->num_cols(), jacobian->num_cols());
|
|
|
|
const CompressedRowBlockStructure* bs = jacobian->block_structure();
|
|
for (int i = 0; i < bs->cols.size(); ++i) {
|
|
const int block_size = bs->cols[i].size;
|
|
int r, c, row_stride, col_stride;
|
|
CellInfo* cell_info = m->GetCell(i, i, &r, &c, &row_stride, &col_stride);
|
|
Matrix actual_block_inverse =
|
|
MatrixRef(cell_info->values, row_stride, col_stride)
|
|
.block(r, c, block_size, block_size);
|
|
Matrix expected_block = hessian.block(
|
|
bs->cols[i].position, bs->cols[i].position, block_size, block_size);
|
|
const double residual = (actual_block_inverse * expected_block -
|
|
Matrix::Identity(block_size, block_size))
|
|
.norm();
|
|
EXPECT_NEAR(residual, 0.0, 1e-12) << "Block: " << i;
|
|
}
|
|
options.num_col_blocks++;
|
|
options.num_row_blocks++;
|
|
}
|
|
}
|
|
|
|
TEST(CompressedRowSparseJacobiPreconditioner, _) {
|
|
constexpr int kNumtrials = 10;
|
|
CompressedRowSparseMatrix::RandomMatrixOptions options;
|
|
options.num_col_blocks = 3;
|
|
options.min_col_block_size = 1;
|
|
options.max_col_block_size = 3;
|
|
|
|
options.num_row_blocks = 5;
|
|
options.min_row_block_size = 1;
|
|
options.max_row_block_size = 4;
|
|
options.block_density = 0.25;
|
|
std::mt19937 prng;
|
|
|
|
Preconditioner::Options preconditioner_options;
|
|
ContextImpl context;
|
|
preconditioner_options.context = &context;
|
|
|
|
for (int trial = 0; trial < kNumtrials; ++trial) {
|
|
auto jacobian =
|
|
CompressedRowSparseMatrix::CreateRandomMatrix(options, prng);
|
|
Vector diagonal = Vector::Ones(jacobian->num_cols());
|
|
|
|
Matrix dense_jacobian;
|
|
jacobian->ToDenseMatrix(&dense_jacobian);
|
|
Matrix hessian = dense_jacobian.transpose() * dense_jacobian;
|
|
hessian.diagonal() += diagonal.array().square().matrix();
|
|
|
|
BlockCRSJacobiPreconditioner pre(preconditioner_options, *jacobian);
|
|
pre.Update(*jacobian, diagonal.data());
|
|
auto& m = pre.matrix();
|
|
|
|
EXPECT_EQ(m.num_rows(), jacobian->num_cols());
|
|
EXPECT_EQ(m.num_cols(), jacobian->num_cols());
|
|
|
|
const auto& col_blocks = jacobian->col_blocks();
|
|
for (int i = 0, col = 0; i < col_blocks.size(); ++i) {
|
|
const int block_size = col_blocks[i].size;
|
|
int idx = m.rows()[col];
|
|
for (int j = 0; j < block_size; ++j) {
|
|
EXPECT_EQ(m.rows()[col + j + 1] - m.rows()[col + j], block_size);
|
|
for (int k = 0; k < block_size; ++k, ++idx) {
|
|
EXPECT_EQ(m.cols()[idx], col + k);
|
|
}
|
|
}
|
|
|
|
ConstMatrixRef actual_block_inverse(
|
|
m.values() + m.rows()[col], block_size, block_size);
|
|
Matrix expected_block = hessian.block(col, col, block_size, block_size);
|
|
const double residual = (actual_block_inverse * expected_block -
|
|
Matrix::Identity(block_size, block_size))
|
|
.norm();
|
|
EXPECT_NEAR(residual, 0.0, 1e-12) << "Block: " << i;
|
|
col += block_size;
|
|
}
|
|
options.num_col_blocks++;
|
|
options.num_row_blocks++;
|
|
}
|
|
}
|
|
|
|
} // namespace ceres::internal
|