cuda_block_sparse_crs_view.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. //
  31. #ifndef CERES_INTERNAL_CUDA_BLOCK_SPARSE_CRS_VIEW_H_
  32. #define CERES_INTERNAL_CUDA_BLOCK_SPARSE_CRS_VIEW_H_
  33. #include "ceres/internal/config.h"
  34. #ifndef CERES_NO_CUDA
  35. #include <memory>
  36. #include "ceres/block_sparse_matrix.h"
  37. #include "ceres/cuda_block_structure.h"
  38. #include "ceres/cuda_buffer.h"
  39. #include "ceres/cuda_sparse_matrix.h"
  40. #include "ceres/cuda_streamed_buffer.h"
  41. namespace ceres::internal {
  42. // We use cuSPARSE library for SpMV operations. However, it does not support
  43. // block-sparse format with varying size of the blocks. Thus, we perform the
  44. // following operations in order to compute products of block-sparse matrices
  45. // and dense vectors on gpu:
  46. // - Once per block-sparse structure update:
  47. // - Compute CRS structure from block-sparse structure and check if values of
  48. // block-sparse matrix would have the same order as values of CRS matrix
  49. // - Once per block-sparse values update:
  50. // - Update values in CRS matrix with values of block-sparse matrix
  51. //
  52. // Only block-sparse matrices with sequential order of cells are supported.
  53. //
  54. // UpdateValues method updates values:
  55. // - In a single host-to-device copy for matrices with CRS-compatible value
  56. // layout
  57. // - Simultaneously transferring and permuting values using CudaStreamedBuffer
  58. // otherwise
  59. class CERES_NO_EXPORT CudaBlockSparseCRSView {
  60. public:
  61. // Initializes internal CRS matrix using structure and values of block-sparse
  62. // matrix For block-sparse matrices that have value layout different from CRS
  63. // block-sparse structure will be stored/
  64. CudaBlockSparseCRSView(const BlockSparseMatrix& bsm, ContextImpl* context);
  65. const CudaSparseMatrix* crs_matrix() const { return crs_matrix_.get(); }
  66. CudaSparseMatrix* mutable_crs_matrix() { return crs_matrix_.get(); }
  67. // Update values of crs_matrix_ using values of block-sparse matrix.
  68. // Assumes that bsm has the same block-sparse structure as matrix that was
  69. // used for construction.
  70. void UpdateValues(const BlockSparseMatrix& bsm);
  71. // Returns true if block-sparse matrix had CRS-compatible value layout
  72. bool IsCrsCompatible() const { return is_crs_compatible_; }
  73. void LeftMultiplyAndAccumulate(const CudaVector& x, CudaVector* y) const {
  74. crs_matrix()->LeftMultiplyAndAccumulate(x, y);
  75. }
  76. void RightMultiplyAndAccumulate(const CudaVector& x, CudaVector* y) const {
  77. crs_matrix()->RightMultiplyAndAccumulate(x, y);
  78. }
  79. private:
  80. // Value permutation kernel performs a single element-wise operation per
  81. // thread, thus performing permutation in blocks of 8 megabytes of
  82. // block-sparse values seems reasonable
  83. static constexpr int kMaxTemporaryArraySize = 1 * 1024 * 1024;
  84. std::unique_ptr<CudaSparseMatrix> crs_matrix_;
  85. // Only created if block-sparse matrix has non-CRS value layout
  86. std::unique_ptr<CudaStreamedBuffer<double>> streamed_buffer_;
  87. // Only stored if block-sparse matrix has non-CRS value layout
  88. std::unique_ptr<CudaBlockSparseStructure> block_structure_;
  89. bool is_crs_compatible_;
  90. ContextImpl* context_;
  91. };
  92. } // namespace ceres::internal
  93. #endif // CERES_NO_CUDA
  94. #endif // CERES_INTERNAL_CUDA_BLOCK_SPARSE_CRS_VIEW_H_