cuda_vector.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 simple CUDA vector class.
  32. // This include must come before any #ifndef check on Ceres compile options.
  33. // clang-format off
  34. #include "ceres/internal/config.h"
  35. // clang-format on
  36. #include <math.h>
  37. #include "ceres/context_impl.h"
  38. #include "ceres/internal/export.h"
  39. #include "ceres/types.h"
  40. #ifndef CERES_NO_CUDA
  41. #include "ceres/cuda_buffer.h"
  42. #include "ceres/cuda_kernels_vector_ops.h"
  43. #include "ceres/cuda_vector.h"
  44. #include "cublas_v2.h"
  45. namespace ceres::internal {
  46. CudaVector::CudaVector(ContextImpl* context, int size)
  47. : context_(context), data_(context, size) {
  48. DCHECK_NE(context, nullptr);
  49. DCHECK(context->IsCudaInitialized());
  50. Resize(size);
  51. }
  52. CudaVector& CudaVector::operator=(const CudaVector& other) {
  53. if (this != &other) {
  54. Resize(other.num_rows());
  55. data_.CopyFromGPUArray(other.data_.data(), num_rows_);
  56. }
  57. return *this;
  58. }
  59. void CudaVector::DestroyDescriptor() {
  60. if (descr_ != nullptr) {
  61. CHECK_EQ(cusparseDestroyDnVec(descr_), CUSPARSE_STATUS_SUCCESS);
  62. descr_ = nullptr;
  63. }
  64. }
  65. CudaVector::~CudaVector() { DestroyDescriptor(); }
  66. void CudaVector::Resize(int size) {
  67. data_.Reserve(size);
  68. num_rows_ = size;
  69. DestroyDescriptor();
  70. CHECK_EQ(cusparseCreateDnVec(&descr_, num_rows_, data_.data(), CUDA_R_64F),
  71. CUSPARSE_STATUS_SUCCESS);
  72. }
  73. double CudaVector::Dot(const CudaVector& x) const {
  74. double result = 0;
  75. CHECK_EQ(cublasDdot(context_->cublas_handle_,
  76. num_rows_,
  77. data_.data(),
  78. 1,
  79. x.data().data(),
  80. 1,
  81. &result),
  82. CUBLAS_STATUS_SUCCESS)
  83. << "CuBLAS cublasDdot failed.";
  84. return result;
  85. }
  86. double CudaVector::Norm() const {
  87. double result = 0;
  88. CHECK_EQ(cublasDnrm2(
  89. context_->cublas_handle_, num_rows_, data_.data(), 1, &result),
  90. CUBLAS_STATUS_SUCCESS)
  91. << "CuBLAS cublasDnrm2 failed.";
  92. return result;
  93. }
  94. void CudaVector::CopyFromCpu(const Vector& x) {
  95. data_.Reserve(x.rows());
  96. data_.CopyFromCpu(x.data(), x.rows());
  97. num_rows_ = x.rows();
  98. DestroyDescriptor();
  99. CHECK_EQ(cusparseCreateDnVec(&descr_, num_rows_, data_.data(), CUDA_R_64F),
  100. CUSPARSE_STATUS_SUCCESS);
  101. }
  102. void CudaVector::CopyTo(Vector* x) const {
  103. CHECK(x != nullptr);
  104. x->resize(num_rows_);
  105. data_.CopyToCpu(x->data(), num_rows_);
  106. }
  107. void CudaVector::CopyTo(double* x) const {
  108. CHECK(x != nullptr);
  109. data_.CopyToCpu(x, num_rows_);
  110. }
  111. void CudaVector::SetZero() {
  112. CHECK(data_.data() != nullptr);
  113. CudaSetZeroFP64(data_.data(), num_rows_, context_->DefaultStream());
  114. }
  115. void CudaVector::Axpby(double a, const CudaVector& x, double b) {
  116. if (&x == this) {
  117. Scale(a + b);
  118. return;
  119. }
  120. CHECK_EQ(num_rows_, x.num_rows_);
  121. if (b != 1.0) {
  122. // First scale y by b.
  123. CHECK_EQ(
  124. cublasDscal(context_->cublas_handle_, num_rows_, &b, data_.data(), 1),
  125. CUBLAS_STATUS_SUCCESS)
  126. << "CuBLAS cublasDscal failed.";
  127. }
  128. // Then add a * x to y.
  129. CHECK_EQ(cublasDaxpy(context_->cublas_handle_,
  130. num_rows_,
  131. &a,
  132. x.data().data(),
  133. 1,
  134. data_.data(),
  135. 1),
  136. CUBLAS_STATUS_SUCCESS)
  137. << "CuBLAS cublasDaxpy failed.";
  138. }
  139. void CudaVector::DtDxpy(const CudaVector& D, const CudaVector& x) {
  140. CudaDtDxpy(data_.data(),
  141. D.data().data(),
  142. x.data().data(),
  143. num_rows_,
  144. context_->DefaultStream());
  145. }
  146. void CudaVector::Scale(double s) {
  147. CHECK_EQ(
  148. cublasDscal(context_->cublas_handle_, num_rows_, &s, data_.data(), 1),
  149. CUBLAS_STATUS_SUCCESS)
  150. << "CuBLAS cublasDscal failed.";
  151. }
  152. } // namespace ceres::internal
  153. #endif // CERES_NO_CUDA