block_random_access_sparse_matrix.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #include "ceres/block_random_access_sparse_matrix.h"
  31. #include <algorithm>
  32. #include <memory>
  33. #include <set>
  34. #include <utility>
  35. #include <vector>
  36. #include "ceres/internal/export.h"
  37. #include "ceres/parallel_vector_ops.h"
  38. #include "ceres/triplet_sparse_matrix.h"
  39. #include "ceres/types.h"
  40. #include "glog/logging.h"
  41. namespace ceres::internal {
  42. BlockRandomAccessSparseMatrix::BlockRandomAccessSparseMatrix(
  43. const std::vector<Block>& blocks,
  44. const std::set<std::pair<int, int>>& block_pairs,
  45. ContextImpl* context,
  46. int num_threads)
  47. : blocks_(blocks), context_(context), num_threads_(num_threads) {
  48. CHECK_LE(blocks.size(), std::numeric_limits<std::int32_t>::max());
  49. const int num_cols = NumScalarEntries(blocks);
  50. const int num_blocks = blocks.size();
  51. std::vector<int> num_cells_at_row(num_blocks);
  52. for (auto& p : block_pairs) {
  53. ++num_cells_at_row[p.first];
  54. }
  55. auto block_structure_ = new CompressedRowBlockStructure;
  56. block_structure_->cols = blocks;
  57. block_structure_->rows.resize(num_blocks);
  58. auto p = block_pairs.begin();
  59. int num_nonzeros = 0;
  60. // Pairs of block indices are sorted lexicographically, thus pairs
  61. // corresponding to a single row-block are stored in segments of index pairs
  62. // with constant row-block index and increasing column-block index.
  63. // CompressedRowBlockStructure is created by traversing block_pairs set.
  64. for (int row_block_id = 0; row_block_id < num_blocks; ++row_block_id) {
  65. auto& row = block_structure_->rows[row_block_id];
  66. row.block = blocks[row_block_id];
  67. row.cells.reserve(num_cells_at_row[row_block_id]);
  68. const int row_block_size = blocks[row_block_id].size;
  69. // Process all index pairs corresponding to the current row block. Because
  70. // index pairs are sorted lexicographically, cells are being appended to the
  71. // current row-block till the first change in row-block index
  72. for (; p != block_pairs.end() && row_block_id == p->first; ++p) {
  73. const int col_block_id = p->second;
  74. row.cells.emplace_back(col_block_id, num_nonzeros);
  75. num_nonzeros += row_block_size * blocks[col_block_id].size;
  76. }
  77. }
  78. bsm_ = std::make_unique<BlockSparseMatrix>(block_structure_);
  79. VLOG(1) << "Matrix Size [" << num_cols << "," << num_cols << "] "
  80. << num_nonzeros;
  81. double* values = bsm_->mutable_values();
  82. for (int row_block_id = 0; row_block_id < num_blocks; ++row_block_id) {
  83. const auto& cells = block_structure_->rows[row_block_id].cells;
  84. for (auto& c : cells) {
  85. const int col_block_id = c.block_id;
  86. double* const data = values + c.position;
  87. layout_[IntPairToInt64(row_block_id, col_block_id)] =
  88. std::make_unique<CellInfo>(data);
  89. }
  90. }
  91. }
  92. CellInfo* BlockRandomAccessSparseMatrix::GetCell(int row_block_id,
  93. int col_block_id,
  94. int* row,
  95. int* col,
  96. int* row_stride,
  97. int* col_stride) {
  98. const auto it = layout_.find(IntPairToInt64(row_block_id, col_block_id));
  99. if (it == layout_.end()) {
  100. return nullptr;
  101. }
  102. // Each cell is stored contiguously as its own little dense matrix.
  103. *row = 0;
  104. *col = 0;
  105. *row_stride = blocks_[row_block_id].size;
  106. *col_stride = blocks_[col_block_id].size;
  107. return it->second.get();
  108. }
  109. // Assume that the user does not hold any locks on any cell blocks
  110. // when they are calling SetZero.
  111. void BlockRandomAccessSparseMatrix::SetZero() {
  112. bsm_->SetZero(context_, num_threads_);
  113. }
  114. void BlockRandomAccessSparseMatrix::SymmetricRightMultiplyAndAccumulate(
  115. const double* x, double* y) const {
  116. const auto bs = bsm_->block_structure();
  117. const auto values = bsm_->values();
  118. const int num_blocks = blocks_.size();
  119. for (int row_block_id = 0; row_block_id < num_blocks; ++row_block_id) {
  120. const auto& row_block = bs->rows[row_block_id];
  121. const int row_block_size = row_block.block.size;
  122. const int row_block_pos = row_block.block.position;
  123. for (auto& c : row_block.cells) {
  124. const int col_block_id = c.block_id;
  125. const int col_block_size = blocks_[col_block_id].size;
  126. const int col_block_pos = blocks_[col_block_id].position;
  127. MatrixVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
  128. values + c.position,
  129. row_block_size,
  130. col_block_size,
  131. x + col_block_pos,
  132. y + row_block_pos);
  133. if (col_block_id == row_block_id) {
  134. continue;
  135. }
  136. // Since the matrix is symmetric, but only the upper triangular
  137. // part is stored, if the block being accessed is not a diagonal
  138. // block, then use the same block to do the corresponding lower
  139. // triangular multiply also
  140. MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
  141. values + c.position,
  142. row_block_size,
  143. col_block_size,
  144. x + row_block_pos,
  145. y + col_block_pos);
  146. }
  147. }
  148. }
  149. } // namespace ceres::internal