blob: 83c05d40fd65174afb53ae0ea40ad7c201aa0f66 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh1d1e6ea2020-12-23 21:56:30 -080031#include "ceres/preprocessor.h"
32
Austin Schuh3de38b02024-06-25 18:25:10 -070033#include <memory>
34
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include "ceres/callbacks.h"
36#include "ceres/gradient_checking_cost_function.h"
37#include "ceres/line_search_preprocessor.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/problem_impl.h"
39#include "ceres/solver.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070040#include "ceres/thread_pool.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/trust_region_preprocessor.h"
42
Austin Schuh3de38b02024-06-25 18:25:10 -070043namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080044
Austin Schuh3de38b02024-06-25 18:25:10 -070045std::unique_ptr<Preprocessor> Preprocessor::Create(
46 MinimizerType minimizer_type) {
Austin Schuh70cc9552019-01-21 19:46:48 -080047 if (minimizer_type == TRUST_REGION) {
Austin Schuh3de38b02024-06-25 18:25:10 -070048 return std::make_unique<TrustRegionPreprocessor>();
Austin Schuh70cc9552019-01-21 19:46:48 -080049 }
50
51 if (minimizer_type == LINE_SEARCH) {
Austin Schuh3de38b02024-06-25 18:25:10 -070052 return std::make_unique<LineSearchPreprocessor>();
Austin Schuh70cc9552019-01-21 19:46:48 -080053 }
54
55 LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type;
Austin Schuh3de38b02024-06-25 18:25:10 -070056 return nullptr;
Austin Schuh70cc9552019-01-21 19:46:48 -080057}
58
Austin Schuh3de38b02024-06-25 18:25:10 -070059Preprocessor::~Preprocessor() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080060
61void ChangeNumThreadsIfNeeded(Solver::Options* options) {
Austin Schuh3de38b02024-06-25 18:25:10 -070062 if (options->num_threads == 1) {
63 return;
64 }
65 const int num_threads_available = ThreadPool::MaxNumThreadsAvailable();
Austin Schuh70cc9552019-01-21 19:46:48 -080066 if (options->num_threads > num_threads_available) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080067 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 Schuh70cc9552019-01-21 19:46:48 -080071 options->num_threads = num_threads_available;
72 }
73}
74
75void 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 Schuh3de38b02024-06-25 18:25:10 -070085 auto context = pp->problem->context();
Austin Schuh70cc9552019-01-21 19:46:48 -080086 Minimizer::Options& minimizer_options = pp->minimizer_options;
87 minimizer_options = Minimizer::Options(options);
88 minimizer_options.evaluator = pp->evaluator;
Austin Schuh3de38b02024-06-25 18:25:10 -070089 minimizer_options.context = context;
Austin Schuh70cc9552019-01-21 19:46:48 -080090
91 if (options.logging_type != SILENT) {
Austin Schuh3de38b02024-06-25 18:25:10 -070092 pp->logging_callback = std::make_unique<LoggingCallback>(
93 options.minimizer_type, options.minimizer_progress_to_stdout);
Austin Schuh70cc9552019-01-21 19:46:48 -080094 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
95 pp->logging_callback.get());
96 }
97
98 if (options.update_state_every_iteration) {
Austin Schuh3de38b02024-06-25 18:25:10 -070099 pp->state_updating_callback =
100 std::make_unique<StateUpdatingCallback>(program, reduced_parameters);
Austin Schuh70cc9552019-01-21 19:46:48 -0800101 // 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 Schuh3de38b02024-06-25 18:25:10 -0700108} // namespace ceres::internal