cuda_block_structure_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Authors: dmitriy.korchemkin@gmail.com (Dmitriy Korchemkin)
  30. #include "ceres/internal/config.h"
  31. #ifndef CERES_NO_CUDA
  32. #include <glog/logging.h>
  33. #include <gtest/gtest.h>
  34. #include <numeric>
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/cuda_block_structure.h"
  37. namespace ceres::internal {
  38. class CudaBlockStructureTest : public ::testing::Test {
  39. protected:
  40. void SetUp() final {
  41. std::string message;
  42. CHECK(context_.InitCuda(&message))
  43. << "InitCuda() failed because: " << message;
  44. BlockSparseMatrix::RandomMatrixOptions options;
  45. options.num_row_blocks = 1234;
  46. options.min_row_block_size = 1;
  47. options.max_row_block_size = 10;
  48. options.num_col_blocks = 567;
  49. options.min_col_block_size = 1;
  50. options.max_col_block_size = 10;
  51. options.block_density = 0.2;
  52. std::mt19937 rng;
  53. A_ = BlockSparseMatrix::CreateRandomMatrix(options, rng);
  54. std::iota(
  55. A_->mutable_values(), A_->mutable_values() + A_->num_nonzeros(), 1);
  56. }
  57. std::vector<Cell> GetCells(const CudaBlockSparseStructure& structure) {
  58. const auto& cuda_buffer = structure.cells_;
  59. std::vector<Cell> cells(cuda_buffer.size());
  60. cuda_buffer.CopyToCpu(cells.data(), cells.size());
  61. return cells;
  62. }
  63. std::vector<Block> GetRowBlocks(const CudaBlockSparseStructure& structure) {
  64. const auto& cuda_buffer = structure.row_blocks_;
  65. std::vector<Block> blocks(cuda_buffer.size());
  66. cuda_buffer.CopyToCpu(blocks.data(), blocks.size());
  67. return blocks;
  68. }
  69. std::vector<Block> GetColBlocks(const CudaBlockSparseStructure& structure) {
  70. const auto& cuda_buffer = structure.col_blocks_;
  71. std::vector<Block> blocks(cuda_buffer.size());
  72. cuda_buffer.CopyToCpu(blocks.data(), blocks.size());
  73. return blocks;
  74. }
  75. std::vector<int> GetRowBlockOffsets(
  76. const CudaBlockSparseStructure& structure) {
  77. const auto& cuda_buffer = structure.first_cell_in_row_block_;
  78. std::vector<int> first_cell_in_row_block(cuda_buffer.size());
  79. cuda_buffer.CopyToCpu(first_cell_in_row_block.data(),
  80. first_cell_in_row_block.size());
  81. return first_cell_in_row_block;
  82. }
  83. std::unique_ptr<BlockSparseMatrix> A_;
  84. ContextImpl context_;
  85. };
  86. TEST_F(CudaBlockStructureTest, StructureIdentity) {
  87. auto block_structure = A_->block_structure();
  88. const int num_row_blocks = block_structure->rows.size();
  89. const int num_col_blocks = block_structure->cols.size();
  90. CudaBlockSparseStructure cuda_block_structure(*block_structure, &context_);
  91. ASSERT_EQ(cuda_block_structure.num_rows(), A_->num_rows());
  92. ASSERT_EQ(cuda_block_structure.num_cols(), A_->num_cols());
  93. ASSERT_EQ(cuda_block_structure.num_nonzeros(), A_->num_nonzeros());
  94. ASSERT_EQ(cuda_block_structure.num_row_blocks(), num_row_blocks);
  95. ASSERT_EQ(cuda_block_structure.num_col_blocks(), num_col_blocks);
  96. std::vector<Block> blocks = GetColBlocks(cuda_block_structure);
  97. ASSERT_EQ(blocks.size(), num_col_blocks);
  98. for (int i = 0; i < num_col_blocks; ++i) {
  99. EXPECT_EQ(block_structure->cols[i].position, blocks[i].position);
  100. EXPECT_EQ(block_structure->cols[i].size, blocks[i].size);
  101. }
  102. std::vector<Cell> cells = GetCells(cuda_block_structure);
  103. std::vector<int> first_cell_in_row_block =
  104. GetRowBlockOffsets(cuda_block_structure);
  105. blocks = GetRowBlocks(cuda_block_structure);
  106. ASSERT_EQ(blocks.size(), num_row_blocks);
  107. ASSERT_EQ(first_cell_in_row_block.size(), num_row_blocks + 1);
  108. ASSERT_EQ(first_cell_in_row_block.back(), cells.size());
  109. for (int i = 0; i < num_row_blocks; ++i) {
  110. const int num_cells = block_structure->rows[i].cells.size();
  111. EXPECT_EQ(blocks[i].position, block_structure->rows[i].block.position);
  112. EXPECT_EQ(blocks[i].size, block_structure->rows[i].block.size);
  113. const int first_cell = first_cell_in_row_block[i];
  114. const int last_cell = first_cell_in_row_block[i + 1];
  115. ASSERT_EQ(last_cell - first_cell, num_cells);
  116. for (int j = 0; j < num_cells; ++j) {
  117. EXPECT_EQ(cells[first_cell + j].block_id,
  118. block_structure->rows[i].cells[j].block_id);
  119. EXPECT_EQ(cells[first_cell + j].position,
  120. block_structure->rows[i].cells[j].position);
  121. }
  122. }
  123. }
  124. } // namespace ceres::internal
  125. #endif // CERES_NO_CUDA