eigen_vector_ops.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Author: sameeragarwal@google.com (Sameer Agarwal)
  30. #ifndef CERES_INTERNAL_EIGEN_VECTOR_OPS_H_
  31. #define CERES_INTERNAL_EIGEN_VECTOR_OPS_H_
  32. #include <numeric>
  33. #include "ceres/internal/eigen.h"
  34. #include "ceres/internal/fixed_array.h"
  35. #include "ceres/parallel_for.h"
  36. #include "ceres/parallel_vector_ops.h"
  37. namespace ceres::internal {
  38. // Blas1 operations on Eigen vectors. These functions are needed as an
  39. // abstraction layer so that we can use different versions of a vector style
  40. // object in the conjugate gradients linear solver.
  41. template <typename Derived>
  42. inline double Norm(const Eigen::DenseBase<Derived>& x,
  43. ContextImpl* context,
  44. int num_threads) {
  45. FixedArray<double> norms(num_threads, 0.);
  46. ParallelFor(
  47. context,
  48. 0,
  49. x.rows(),
  50. num_threads,
  51. [&x, &norms](int thread_id, std::tuple<int, int> range) {
  52. auto [start, end] = range;
  53. norms[thread_id] += x.segment(start, end - start).squaredNorm();
  54. },
  55. kMinBlockSizeParallelVectorOps);
  56. return std::sqrt(std::accumulate(norms.begin(), norms.end(), 0.));
  57. }
  58. inline void SetZero(Vector& x, ContextImpl* context, int num_threads) {
  59. ParallelSetZero(context, num_threads, x);
  60. }
  61. inline void Axpby(double a,
  62. const Vector& x,
  63. double b,
  64. const Vector& y,
  65. Vector& z,
  66. ContextImpl* context,
  67. int num_threads) {
  68. ParallelAssign(context, num_threads, z, a * x + b * y);
  69. }
  70. template <typename VectorLikeX, typename VectorLikeY>
  71. inline double Dot(const VectorLikeX& x,
  72. const VectorLikeY& y,
  73. ContextImpl* context,
  74. int num_threads) {
  75. FixedArray<double> dots(num_threads, 0.);
  76. ParallelFor(
  77. context,
  78. 0,
  79. x.rows(),
  80. num_threads,
  81. [&x, &y, &dots](int thread_id, std::tuple<int, int> range) {
  82. auto [start, end] = range;
  83. const int block_size = end - start;
  84. const auto& x_block = x.segment(start, block_size);
  85. const auto& y_block = y.segment(start, block_size);
  86. dots[thread_id] += x_block.dot(y_block);
  87. },
  88. kMinBlockSizeParallelVectorOps);
  89. return std::accumulate(dots.begin(), dots.end(), 0.);
  90. }
  91. inline void Copy(const Vector& from,
  92. Vector& to,
  93. ContextImpl* context,
  94. int num_threads) {
  95. ParallelAssign(context, num_threads, to, from);
  96. }
  97. } // namespace ceres::internal
  98. #endif // CERES_INTERNAL_EIGEN_VECTOR_OPS_H_