2023-05-03 20:58:35 +03:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
|
|
|
|
// Copyright 2023 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.
|
|
|
|
|
//
|
|
|
|
|
// Authors: dmitriy.korchemkin@gmail.com (Dmitriy Korchemkin)
|
|
|
|
|
|
|
|
|
|
#include "ceres/cuda_block_structure.h"
|
|
|
|
|
|
|
|
|
|
#ifndef CERES_NO_CUDA
|
|
|
|
|
|
2024-07-18 11:37:07 -07:00
|
|
|
#include "absl/log/check.h"
|
|
|
|
|
#include "absl/log/log.h"
|
2024-08-09 16:15:22 -07:00
|
|
|
#include "absl/log/vlog_is_on.h"
|
2024-07-18 11:37:07 -07:00
|
|
|
|
2023-05-03 20:58:35 +03:00
|
|
|
namespace ceres::internal {
|
|
|
|
|
namespace {
|
|
|
|
|
// Dimension of a sorted array of blocks
|
|
|
|
|
inline int Dimension(const std::vector<Block>& blocks) {
|
|
|
|
|
if (blocks.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const auto& last = blocks.back();
|
|
|
|
|
return last.size + last.position;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
CudaBlockSparseStructure::CudaBlockSparseStructure(
|
|
|
|
|
const CompressedRowBlockStructure& block_structure, ContextImpl* context)
|
2023-06-19 18:00:28 +00:00
|
|
|
: CudaBlockSparseStructure(block_structure, 0, context) {}
|
|
|
|
|
|
|
|
|
|
CudaBlockSparseStructure::CudaBlockSparseStructure(
|
|
|
|
|
const CompressedRowBlockStructure& block_structure,
|
|
|
|
|
const int num_col_blocks_e,
|
|
|
|
|
ContextImpl* context)
|
2023-05-17 14:43:33 +03:00
|
|
|
: first_cell_in_row_block_(context),
|
2023-06-19 18:00:28 +00:00
|
|
|
value_offset_row_block_f_(context),
|
2023-05-03 20:58:35 +03:00
|
|
|
cells_(context),
|
|
|
|
|
row_blocks_(context),
|
|
|
|
|
col_blocks_(context) {
|
|
|
|
|
// Row blocks extracted from CompressedRowBlockStructure::rows
|
|
|
|
|
std::vector<Block> row_blocks;
|
|
|
|
|
// Column blocks can be reused as-is
|
|
|
|
|
const auto& col_blocks = block_structure.cols;
|
|
|
|
|
|
|
|
|
|
// Row block offset is an index of the first cell corresponding to row block
|
2023-05-17 14:43:33 +03:00
|
|
|
std::vector<int> first_cell_in_row_block;
|
2023-06-19 18:00:28 +00:00
|
|
|
// Offset of the first value in the first non-empty row-block of F sub-matrix
|
|
|
|
|
std::vector<int> value_offset_row_block_f;
|
2023-05-03 20:58:35 +03:00
|
|
|
// Flat array of all cells from all row-blocks
|
|
|
|
|
std::vector<Cell> cells;
|
|
|
|
|
|
2023-06-19 18:00:28 +00:00
|
|
|
int f_values_offset = -1;
|
|
|
|
|
num_nonzeros_e_ = 0;
|
2023-05-17 14:43:33 +03:00
|
|
|
is_crs_compatible_ = true;
|
2023-05-03 20:58:35 +03:00
|
|
|
num_row_blocks_ = block_structure.rows.size();
|
|
|
|
|
num_col_blocks_ = col_blocks.size();
|
|
|
|
|
|
|
|
|
|
row_blocks.reserve(num_row_blocks_);
|
2023-05-17 14:43:33 +03:00
|
|
|
first_cell_in_row_block.reserve(num_row_blocks_ + 1);
|
2023-06-19 18:00:28 +00:00
|
|
|
value_offset_row_block_f.reserve(num_row_blocks_ + 1);
|
2023-05-03 20:58:35 +03:00
|
|
|
num_nonzeros_ = 0;
|
2023-06-19 18:00:28 +00:00
|
|
|
// Block-sparse matrices arising from block-jacobian writer are expected to
|
|
|
|
|
// have sequential layout (for partitioned matrices - it is expected that both
|
|
|
|
|
// E and F sub-matrices have sequential layout).
|
|
|
|
|
bool sequential_layout = true;
|
|
|
|
|
int row_block_id = 0;
|
|
|
|
|
num_row_blocks_e_ = 0;
|
|
|
|
|
for (; row_block_id < num_row_blocks_; ++row_block_id) {
|
|
|
|
|
const auto& r = block_structure.rows[row_block_id];
|
2023-05-03 20:58:35 +03:00
|
|
|
const int row_block_size = r.block.size;
|
2023-06-19 18:00:28 +00:00
|
|
|
const int num_cells = r.cells.size();
|
|
|
|
|
|
|
|
|
|
if (num_col_blocks_e == 0 || r.cells.size() == 0 ||
|
|
|
|
|
r.cells[0].block_id >= num_col_blocks_e) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
num_row_blocks_e_ = row_block_id + 1;
|
|
|
|
|
// In E sub-matrix there is exactly a single E cell in the row
|
2024-07-21 15:15:36 -07:00
|
|
|
// since E cells are stored separately from F cells, crs-compatibility of
|
2023-06-19 18:00:28 +00:00
|
|
|
// F sub-matrix only breaks if there are more than 2 cells in row (that
|
|
|
|
|
// is, more than 1 cell in F sub-matrix)
|
|
|
|
|
if (num_cells > 2 && row_block_size > 1) {
|
2023-05-17 14:43:33 +03:00
|
|
|
is_crs_compatible_ = false;
|
|
|
|
|
}
|
2023-05-03 20:58:35 +03:00
|
|
|
row_blocks.emplace_back(r.block);
|
2023-05-17 14:43:33 +03:00
|
|
|
first_cell_in_row_block.push_back(cells.size());
|
2023-06-19 18:00:28 +00:00
|
|
|
|
|
|
|
|
for (int cell_id = 0; cell_id < num_cells; ++cell_id) {
|
|
|
|
|
const auto& c = r.cells[cell_id];
|
|
|
|
|
const int col_block_size = col_blocks[c.block_id].size;
|
|
|
|
|
const int cell_size = col_block_size * row_block_size;
|
|
|
|
|
cells.push_back(c);
|
|
|
|
|
if (cell_id == 0) {
|
|
|
|
|
DCHECK(c.position == num_nonzeros_e_);
|
|
|
|
|
num_nonzeros_e_ += cell_size;
|
|
|
|
|
} else {
|
|
|
|
|
if (f_values_offset == -1) {
|
|
|
|
|
num_nonzeros_ = c.position;
|
|
|
|
|
f_values_offset = c.position;
|
|
|
|
|
}
|
|
|
|
|
sequential_layout &= c.position == num_nonzeros_;
|
|
|
|
|
num_nonzeros_ += cell_size;
|
|
|
|
|
if (cell_id == 1) {
|
|
|
|
|
// Correct value_offset_row_block_f for empty row-blocks of F
|
|
|
|
|
// preceding this one
|
|
|
|
|
for (auto it = value_offset_row_block_f.rbegin();
|
|
|
|
|
it != value_offset_row_block_f.rend();
|
|
|
|
|
++it) {
|
|
|
|
|
if (*it != -1) break;
|
|
|
|
|
*it = c.position;
|
|
|
|
|
}
|
|
|
|
|
value_offset_row_block_f.push_back(c.position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_cells == 1) {
|
|
|
|
|
value_offset_row_block_f.push_back(-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (; row_block_id < num_row_blocks_; ++row_block_id) {
|
|
|
|
|
const auto& r = block_structure.rows[row_block_id];
|
|
|
|
|
const int row_block_size = r.block.size;
|
|
|
|
|
const int num_cells = r.cells.size();
|
|
|
|
|
// After num_row_blocks_e_ row-blocks, there should be no cells in E
|
|
|
|
|
// sub-matrix. Thus crs-compatibility of F sub-matrix breaks if there are
|
|
|
|
|
// more than one cells in the row-block
|
|
|
|
|
if (num_cells > 1 && row_block_size > 1) {
|
|
|
|
|
is_crs_compatible_ = false;
|
|
|
|
|
}
|
|
|
|
|
row_blocks.emplace_back(r.block);
|
|
|
|
|
first_cell_in_row_block.push_back(cells.size());
|
|
|
|
|
|
|
|
|
|
if (r.cells.empty()) {
|
|
|
|
|
value_offset_row_block_f.push_back(-1);
|
|
|
|
|
} else {
|
|
|
|
|
for (auto it = value_offset_row_block_f.rbegin();
|
|
|
|
|
it != value_offset_row_block_f.rend();
|
|
|
|
|
--it) {
|
|
|
|
|
if (*it != -1) break;
|
|
|
|
|
*it = cells[0].position;
|
|
|
|
|
}
|
|
|
|
|
value_offset_row_block_f.push_back(r.cells[0].position);
|
|
|
|
|
}
|
2023-05-03 20:58:35 +03:00
|
|
|
for (const auto& c : r.cells) {
|
|
|
|
|
const int col_block_size = col_blocks[c.block_id].size;
|
2023-05-17 14:43:33 +03:00
|
|
|
const int cell_size = col_block_size * row_block_size;
|
|
|
|
|
cells.push_back(c);
|
2023-06-19 18:00:28 +00:00
|
|
|
DCHECK(c.block_id >= num_col_blocks_e);
|
|
|
|
|
if (f_values_offset == -1) {
|
|
|
|
|
num_nonzeros_ = c.position;
|
|
|
|
|
f_values_offset = c.position;
|
|
|
|
|
}
|
|
|
|
|
sequential_layout &= c.position == num_nonzeros_;
|
2023-05-17 14:43:33 +03:00
|
|
|
num_nonzeros_ += cell_size;
|
2023-05-03 20:58:35 +03:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-19 18:00:28 +00:00
|
|
|
|
|
|
|
|
if (f_values_offset == -1) {
|
|
|
|
|
f_values_offset = num_nonzeros_e_;
|
|
|
|
|
num_nonzeros_ = num_nonzeros_e_;
|
|
|
|
|
}
|
|
|
|
|
// Fill non-zero offsets for the last rows of F submatrix
|
|
|
|
|
for (auto it = value_offset_row_block_f.rbegin();
|
|
|
|
|
it != value_offset_row_block_f.rend();
|
|
|
|
|
++it) {
|
|
|
|
|
if (*it != -1) break;
|
|
|
|
|
*it = num_nonzeros_;
|
|
|
|
|
}
|
|
|
|
|
value_offset_row_block_f.push_back(num_nonzeros_);
|
|
|
|
|
CHECK_EQ(num_nonzeros_e_, f_values_offset);
|
2023-05-17 14:43:33 +03:00
|
|
|
first_cell_in_row_block.push_back(cells.size());
|
|
|
|
|
num_cells_ = cells.size();
|
2023-05-03 20:58:35 +03:00
|
|
|
|
|
|
|
|
num_rows_ = Dimension(row_blocks);
|
|
|
|
|
num_cols_ = Dimension(col_blocks);
|
|
|
|
|
|
2023-06-19 18:00:28 +00:00
|
|
|
CHECK(sequential_layout);
|
2023-05-17 14:43:33 +03:00
|
|
|
|
2023-05-03 20:58:35 +03:00
|
|
|
if (VLOG_IS_ON(3)) {
|
2023-05-17 14:43:33 +03:00
|
|
|
const size_t first_cell_in_row_block_size =
|
|
|
|
|
first_cell_in_row_block.size() * sizeof(int);
|
2023-05-03 20:58:35 +03:00
|
|
|
const size_t cells_size = cells.size() * sizeof(Cell);
|
|
|
|
|
const size_t row_blocks_size = row_blocks.size() * sizeof(Block);
|
|
|
|
|
const size_t col_blocks_size = col_blocks.size() * sizeof(Block);
|
2023-05-17 14:43:33 +03:00
|
|
|
const size_t total_size = first_cell_in_row_block_size + cells_size +
|
|
|
|
|
col_blocks_size + row_blocks_size;
|
|
|
|
|
const double ratio =
|
|
|
|
|
(100. * total_size) / (num_nonzeros_ * (sizeof(int) + sizeof(double)) +
|
|
|
|
|
num_rows_ * sizeof(int));
|
2023-05-03 20:58:35 +03:00
|
|
|
VLOG(3) << "\nCudaBlockSparseStructure:\n"
|
|
|
|
|
"\tRow block offsets: "
|
2023-05-17 14:43:33 +03:00
|
|
|
<< first_cell_in_row_block_size
|
2023-05-03 20:58:35 +03:00
|
|
|
<< " bytes\n"
|
|
|
|
|
"\tColumn blocks: "
|
|
|
|
|
<< col_blocks_size
|
|
|
|
|
<< " bytes\n"
|
|
|
|
|
"\tRow blocks: "
|
|
|
|
|
<< row_blocks_size
|
|
|
|
|
<< " bytes\n"
|
|
|
|
|
"\tCells: "
|
2023-05-17 14:43:33 +03:00
|
|
|
<< cells_size << " bytes\n\tTotal: " << total_size
|
|
|
|
|
<< " bytes of GPU memory (" << ratio << "% of CRS matrix size)";
|
2023-05-03 20:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 14:43:33 +03:00
|
|
|
first_cell_in_row_block_.CopyFromCpuVector(first_cell_in_row_block);
|
2023-05-03 20:58:35 +03:00
|
|
|
cells_.CopyFromCpuVector(cells);
|
|
|
|
|
row_blocks_.CopyFromCpuVector(row_blocks);
|
|
|
|
|
col_blocks_.CopyFromCpuVector(col_blocks);
|
2023-06-19 18:00:28 +00:00
|
|
|
if (num_col_blocks_e || num_row_blocks_e_) {
|
|
|
|
|
value_offset_row_block_f_.CopyFromCpuVector(value_offset_row_block_f);
|
|
|
|
|
}
|
2023-05-03 20:58:35 +03:00
|
|
|
}
|
|
|
|
|
} // namespace ceres::internal
|
|
|
|
|
|
|
|
|
|
#endif // CERES_NO_CUDA
|