block_random_access_sparse_matrix_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <limits>
  32. #include <memory>
  33. #include <set>
  34. #include <utility>
  35. #include <vector>
  36. #include "ceres/internal/eigen.h"
  37. #include "glog/logging.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres::internal {
  40. TEST(BlockRandomAccessSparseMatrix, GetCell) {
  41. ContextImpl context;
  42. constexpr int num_threads = 1;
  43. std::vector<Block> blocks;
  44. blocks.emplace_back(3, 0);
  45. blocks.emplace_back(4, 3);
  46. blocks.emplace_back(5, 7);
  47. constexpr int num_rows = 3 + 4 + 5;
  48. std::set<std::pair<int, int>> block_pairs;
  49. int num_nonzeros = 0;
  50. block_pairs.emplace(0, 0);
  51. num_nonzeros += blocks[0].size * blocks[0].size;
  52. block_pairs.emplace(1, 1);
  53. num_nonzeros += blocks[1].size * blocks[1].size;
  54. block_pairs.emplace(1, 2);
  55. num_nonzeros += blocks[1].size * blocks[2].size;
  56. block_pairs.emplace(0, 2);
  57. num_nonzeros += blocks[2].size * blocks[0].size;
  58. BlockRandomAccessSparseMatrix m(blocks, block_pairs, &context, num_threads);
  59. EXPECT_EQ(m.num_rows(), num_rows);
  60. EXPECT_EQ(m.num_cols(), num_rows);
  61. for (const auto& block_pair : block_pairs) {
  62. const int row_block_id = block_pair.first;
  63. const int col_block_id = block_pair.second;
  64. int row;
  65. int col;
  66. int row_stride;
  67. int col_stride;
  68. CellInfo* cell = m.GetCell(
  69. row_block_id, col_block_id, &row, &col, &row_stride, &col_stride);
  70. EXPECT_TRUE(cell != nullptr);
  71. EXPECT_EQ(row, 0);
  72. EXPECT_EQ(col, 0);
  73. EXPECT_EQ(row_stride, blocks[row_block_id].size);
  74. EXPECT_EQ(col_stride, blocks[col_block_id].size);
  75. // Write into the block
  76. MatrixRef(cell->values, row_stride, col_stride)
  77. .block(row, col, blocks[row_block_id].size, blocks[col_block_id].size) =
  78. (row_block_id + 1) * (col_block_id + 1) *
  79. Matrix::Ones(blocks[row_block_id].size, blocks[col_block_id].size);
  80. }
  81. const BlockSparseMatrix* bsm = m.matrix();
  82. EXPECT_EQ(bsm->num_nonzeros(), num_nonzeros);
  83. Matrix dense;
  84. bsm->ToDenseMatrix(&dense);
  85. double kTolerance = 1e-14;
  86. // (0, 0)
  87. EXPECT_NEAR(
  88. (dense.block(0, 0, 3, 3) - Matrix::Ones(3, 3)).norm(), 0.0, kTolerance);
  89. // (1, 1)
  90. EXPECT_NEAR((dense.block(3, 3, 4, 4) - 2 * 2 * Matrix::Ones(4, 4)).norm(),
  91. 0.0,
  92. kTolerance);
  93. // (1, 2)
  94. EXPECT_NEAR((dense.block(3, 3 + 4, 4, 5) - 2 * 3 * Matrix::Ones(4, 5)).norm(),
  95. 0.0,
  96. kTolerance);
  97. // (0, 2)
  98. EXPECT_NEAR((dense.block(0, 3 + 4, 3, 5) - 3 * 1 * Matrix::Ones(3, 5)).norm(),
  99. 0.0,
  100. kTolerance);
  101. // There is nothing else in the matrix besides these four blocks.
  102. EXPECT_NEAR(
  103. dense.norm(), sqrt(9. + 16. * 16. + 36. * 20. + 9. * 15.), kTolerance);
  104. Vector x = Vector::Ones(dense.rows());
  105. Vector actual_y = Vector::Zero(dense.rows());
  106. Vector expected_y = Vector::Zero(dense.rows());
  107. expected_y += dense.selfadjointView<Eigen::Upper>() * x;
  108. m.SymmetricRightMultiplyAndAccumulate(x.data(), actual_y.data());
  109. EXPECT_NEAR((expected_y - actual_y).norm(), 0.0, kTolerance)
  110. << "actual: " << actual_y.transpose() << "\n"
  111. << "expected: " << expected_y.transpose() << "matrix: \n " << dense;
  112. }
  113. // IntPairToInt64 is private, thus this fixture is needed to access and
  114. // test it.
  115. class BlockRandomAccessSparseMatrixTest : public ::testing::Test {
  116. public:
  117. void SetUp() final {
  118. std::vector<Block> blocks;
  119. blocks.emplace_back(1, 0);
  120. std::set<std::pair<int, int>> block_pairs;
  121. block_pairs.emplace(0, 0);
  122. m_ = std::make_unique<BlockRandomAccessSparseMatrix>(
  123. blocks, block_pairs, &context_, 1);
  124. }
  125. void CheckIntPairToInt64(int a, int b) {
  126. int64_t value = m_->IntPairToInt64(a, b);
  127. EXPECT_GT(value, 0) << "Overflow a = " << a << " b = " << b;
  128. EXPECT_GT(value, a) << "Overflow a = " << a << " b = " << b;
  129. EXPECT_GT(value, b) << "Overflow a = " << a << " b = " << b;
  130. }
  131. void CheckInt64ToIntPair() {
  132. uint64_t max_rows = m_->kRowShift;
  133. for (int row = max_rows - 10; row < max_rows; ++row) {
  134. for (int col = 0; col < 10; ++col) {
  135. int row_computed;
  136. int col_computed;
  137. m_->Int64ToIntPair(
  138. m_->IntPairToInt64(row, col), &row_computed, &col_computed);
  139. EXPECT_EQ(row, row_computed);
  140. EXPECT_EQ(col, col_computed);
  141. }
  142. }
  143. }
  144. private:
  145. ContextImpl context_;
  146. std::unique_ptr<BlockRandomAccessSparseMatrix> m_;
  147. };
  148. TEST_F(BlockRandomAccessSparseMatrixTest, IntPairToInt64Overflow) {
  149. CheckIntPairToInt64(std::numeric_limits<int32_t>::max(),
  150. std::numeric_limits<int32_t>::max());
  151. }
  152. TEST_F(BlockRandomAccessSparseMatrixTest, Int64ToIntPair) {
  153. CheckInt64ToIntPair();
  154. }
  155. } // namespace ceres::internal