cuda_block_structure.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef CERES_INTERNAL_CUDA_BLOCK_STRUCTURE_H_
  31. #define CERES_INTERNAL_CUDA_BLOCK_STRUCTURE_H_
  32. #include "ceres/internal/config.h"
  33. #ifndef CERES_NO_CUDA
  34. #include "ceres/block_structure.h"
  35. #include "ceres/cuda_buffer.h"
  36. namespace ceres::internal {
  37. class CudaBlockStructureTest;
  38. // This class stores a read-only block-sparse structure in gpu memory.
  39. // Invariants are the same as those of CompressedRowBlockStructure.
  40. // In order to simplify allocation and copying data to gpu, cells from all
  41. // row-blocks are stored in a single array sequentially. Array
  42. // first_cell_in_row_block of size num_row_blocks + 1 allows to identify range
  43. // of cells corresponding to a row-block. Cells corresponding to i-th row-block
  44. // are stored in sub-array cells[first_cell_in_row_block[i]; ...
  45. // first_cell_in_row_block[i + 1] - 1], and their order is preserved.
  46. class CERES_NO_EXPORT CudaBlockSparseStructure {
  47. public:
  48. // CompressedRowBlockStructure is contains a vector of CompressedLists, with
  49. // each CompressedList containing a vector of Cells. We precompute a flat
  50. // array of cells on cpu and transfer it to the gpu.
  51. CudaBlockSparseStructure(const CompressedRowBlockStructure& block_structure,
  52. ContextImpl* context);
  53. // In the case of partitioned matrices, number of non-zeros in E and layout of
  54. // F are computed
  55. CudaBlockSparseStructure(const CompressedRowBlockStructure& block_structure,
  56. const int num_col_blocks_e,
  57. ContextImpl* context);
  58. int num_rows() const { return num_rows_; }
  59. int num_cols() const { return num_cols_; }
  60. int num_cells() const { return num_cells_; }
  61. int num_nonzeros() const { return num_nonzeros_; }
  62. // When partitioned matrix constructor was used, returns number of non-zeros
  63. // in E sub-matrix
  64. int num_nonzeros_e() const { return num_nonzeros_e_; }
  65. int num_row_blocks() const { return num_row_blocks_; }
  66. int num_row_blocks_e() const { return num_row_blocks_e_; }
  67. int num_col_blocks() const { return num_col_blocks_; }
  68. // Returns true if values from block-sparse matrix (F sub-matrix in
  69. // partitioned case) can be copied to CRS matrix as-is. This is possible if
  70. // each row-block is stored in CRS order:
  71. // - Row-block consists of a single row
  72. // - Row-block contains a single cell
  73. bool IsCrsCompatible() const { return is_crs_compatible_; }
  74. // Device pointer to array of num_row_blocks + 1 indices of the first cell of
  75. // row block
  76. const int* first_cell_in_row_block() const {
  77. return first_cell_in_row_block_.data();
  78. }
  79. // Device pointer to array of num_row_blocks + 1 indices of the first value in
  80. // this or subsequent row-blocks of submatrix F
  81. const int* value_offset_row_block_f() const {
  82. return value_offset_row_block_f_.data();
  83. }
  84. // Device pointer to array of num_cells cells, sorted by row-block
  85. const Cell* cells() const { return cells_.data(); }
  86. // Device pointer to array of row blocks
  87. const Block* row_blocks() const { return row_blocks_.data(); }
  88. // Device pointer to array of column blocks
  89. const Block* col_blocks() const { return col_blocks_.data(); }
  90. private:
  91. int num_rows_;
  92. int num_cols_;
  93. int num_cells_;
  94. int num_nonzeros_;
  95. int num_nonzeros_e_;
  96. int num_row_blocks_;
  97. int num_row_blocks_e_;
  98. int num_col_blocks_;
  99. bool is_crs_compatible_;
  100. CudaBuffer<int> first_cell_in_row_block_;
  101. CudaBuffer<int> value_offset_row_block_f_;
  102. CudaBuffer<Cell> cells_;
  103. CudaBuffer<Block> row_blocks_;
  104. CudaBuffer<Block> col_blocks_;
  105. friend class CudaBlockStructureTest;
  106. };
  107. } // namespace ceres::internal
  108. #endif // CERES_NO_CUDA
  109. #endif // CERES_INTERNAL_CUDA_BLOCK_SPARSE_STRUCTURE_H_