Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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: sameragarwal@google.com (Sameer Agarwal) |
| 30 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 31 | #include "ceres/preprocessor.h" |
| 32 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 33 | #include <memory> |
| 34 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | #include "ceres/callbacks.h" |
| 36 | #include "ceres/gradient_checking_cost_function.h" |
| 37 | #include "ceres/line_search_preprocessor.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/problem_impl.h" |
| 39 | #include "ceres/solver.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 40 | #include "ceres/thread_pool.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 41 | #include "ceres/trust_region_preprocessor.h" |
| 42 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | std::unique_ptr<Preprocessor> Preprocessor::Create( |
| 46 | MinimizerType minimizer_type) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 47 | if (minimizer_type == TRUST_REGION) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 48 | return std::make_unique<TrustRegionPreprocessor>(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (minimizer_type == LINE_SEARCH) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 52 | return std::make_unique<LineSearchPreprocessor>(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 56 | return nullptr; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 59 | Preprocessor::~Preprocessor() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | |
| 61 | void ChangeNumThreadsIfNeeded(Solver::Options* options) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 62 | if (options->num_threads == 1) { |
| 63 | return; |
| 64 | } |
| 65 | const int num_threads_available = ThreadPool::MaxNumThreadsAvailable(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 66 | if (options->num_threads > num_threads_available) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 67 | LOG(WARNING) << "Specified options.num_threads: " << options->num_threads |
| 68 | << " exceeds maximum available from the threading model Ceres " |
| 69 | << "was compiled with: " << num_threads_available |
| 70 | << ". Bounding to maximum number available."; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | options->num_threads = num_threads_available; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void SetupCommonMinimizerOptions(PreprocessedProblem* pp) { |
| 76 | const Solver::Options& options = pp->options; |
| 77 | Program* program = pp->reduced_program.get(); |
| 78 | |
| 79 | // Assuming that the parameter blocks in the program have been |
| 80 | // reordered as needed, extract them into a contiguous vector. |
| 81 | pp->reduced_parameters.resize(program->NumParameters()); |
| 82 | double* reduced_parameters = pp->reduced_parameters.data(); |
| 83 | program->ParameterBlocksToStateVector(reduced_parameters); |
| 84 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 85 | auto context = pp->problem->context(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 86 | Minimizer::Options& minimizer_options = pp->minimizer_options; |
| 87 | minimizer_options = Minimizer::Options(options); |
| 88 | minimizer_options.evaluator = pp->evaluator; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 89 | minimizer_options.context = context; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 90 | |
| 91 | if (options.logging_type != SILENT) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 92 | pp->logging_callback = std::make_unique<LoggingCallback>( |
| 93 | options.minimizer_type, options.minimizer_progress_to_stdout); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 94 | minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), |
| 95 | pp->logging_callback.get()); |
| 96 | } |
| 97 | |
| 98 | if (options.update_state_every_iteration) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 99 | pp->state_updating_callback = |
| 100 | std::make_unique<StateUpdatingCallback>(program, reduced_parameters); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 101 | // This must get pushed to the front of the callbacks so that it |
| 102 | // is run before any of the user callbacks. |
| 103 | minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), |
| 104 | pp->state_updating_callback.get()); |
| 105 | } |
| 106 | } |
| 107 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 108 | } // namespace ceres::internal |