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