cuda_sparse_matrix.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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: joydeepb@cs.utexas.edu (Joydeep Biswas)
  30. //
  31. // A CUDA sparse matrix linear operator.
  32. #ifndef CERES_INTERNAL_CUDA_SPARSE_MATRIX_H_
  33. #define CERES_INTERNAL_CUDA_SPARSE_MATRIX_H_
  34. // This include must come before any #ifndef check on Ceres compile options.
  35. // clang-format off
  36. #include "ceres/internal/config.h"
  37. // clang-format on
  38. #include <cstdint>
  39. #include <memory>
  40. #include <string>
  41. #include "ceres/compressed_row_sparse_matrix.h"
  42. #include "ceres/context_impl.h"
  43. #include "ceres/internal/export.h"
  44. #include "ceres/types.h"
  45. #ifndef CERES_NO_CUDA
  46. #include "ceres/cuda_buffer.h"
  47. #include "ceres/cuda_vector.h"
  48. #include "cusparse.h"
  49. namespace ceres::internal {
  50. // A sparse matrix hosted on the GPU in compressed row sparse format, with
  51. // CUDA-accelerated operations.
  52. class CERES_NO_EXPORT CudaSparseMatrix {
  53. public:
  54. // Create a GPU copy of the matrix provided. The caller must ensure that
  55. // InitCuda() has already been successfully called on context before calling
  56. // this constructor.
  57. CudaSparseMatrix(ContextImpl* context,
  58. const CompressedRowSparseMatrix& crs_matrix);
  59. // Creates a "blank" matrix with an appropriate amount of memory allocated.
  60. // The object itself is left in an inconsistent state.
  61. CudaSparseMatrix(int num_rows,
  62. int num_cols,
  63. int num_nonzeros,
  64. ContextImpl* context);
  65. ~CudaSparseMatrix();
  66. // y = y + Ax;
  67. void RightMultiplyAndAccumulate(const CudaVector& x, CudaVector* y);
  68. // y = y + A'x;
  69. void LeftMultiplyAndAccumulate(const CudaVector& x, CudaVector* y);
  70. int num_rows() const { return num_rows_; }
  71. int num_cols() const { return num_cols_; }
  72. int num_nonzeros() const { return num_nonzeros_; }
  73. const int32_t* rows() const { return rows_.data(); }
  74. const int32_t* cols() const { return cols_.data(); }
  75. const double* values() const { return values_.data(); }
  76. int32_t* mutable_rows() { return rows_.data(); }
  77. int32_t* mutable_cols() { return cols_.data(); }
  78. double* mutable_values() { return values_.data(); }
  79. // If subsequent uses of this matrix involve only numerical changes and no
  80. // structural changes, then this method can be used to copy the updated
  81. // non-zero values -- the row and column index arrays are kept the same. It
  82. // is the caller's responsibility to ensure that the sparsity structure of the
  83. // matrix is unchanged.
  84. void CopyValuesFromCpu(const CompressedRowSparseMatrix& crs_matrix);
  85. const cusparseSpMatDescr_t& descr() const { return descr_; }
  86. private:
  87. // Disable copy and assignment.
  88. CudaSparseMatrix(const CudaSparseMatrix&) = delete;
  89. CudaSparseMatrix& operator=(const CudaSparseMatrix&) = delete;
  90. // y = y + op(M)x. op must be either CUSPARSE_OPERATION_NON_TRANSPOSE or
  91. // CUSPARSE_OPERATION_TRANSPOSE.
  92. void SpMv(cusparseOperation_t op, const CudaVector& x, CudaVector* y);
  93. int num_rows_ = 0;
  94. int num_cols_ = 0;
  95. int num_nonzeros_ = 0;
  96. ContextImpl* context_ = nullptr;
  97. // CSR row indices.
  98. CudaBuffer<int32_t> rows_;
  99. // CSR column indices.
  100. CudaBuffer<int32_t> cols_;
  101. // CSR values.
  102. CudaBuffer<double> values_;
  103. // CuSparse object that describes this matrix.
  104. cusparseSpMatDescr_t descr_ = nullptr;
  105. CudaBuffer<uint8_t> spmv_buffer_;
  106. };
  107. } // namespace ceres::internal
  108. #endif // CERES_NO_CUDA
  109. #endif // CERES_INTERNAL_CUDA_SPARSE_MATRIX_H_