blob: 6a67d3856454bfebb2094fe5d91242ee66ea221f [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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: sameragarwal@google.com (Sameer Agarwal)
30
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080031#include "ceres/preprocessor.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include "ceres/callbacks.h"
34#include "ceres/gradient_checking_cost_function.h"
35#include "ceres/line_search_preprocessor.h"
36#include "ceres/parallel_for.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/problem_impl.h"
38#include "ceres/solver.h"
39#include "ceres/trust_region_preprocessor.h"
40
41namespace ceres {
42namespace internal {
43
44Preprocessor* Preprocessor::Create(MinimizerType minimizer_type) {
45 if (minimizer_type == TRUST_REGION) {
46 return new TrustRegionPreprocessor;
47 }
48
49 if (minimizer_type == LINE_SEARCH) {
50 return new LineSearchPreprocessor;
51 }
52
53 LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type;
54 return NULL;
55}
56
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080057Preprocessor::~Preprocessor() {}
Austin Schuh70cc9552019-01-21 19:46:48 -080058
59void ChangeNumThreadsIfNeeded(Solver::Options* options) {
60 const int num_threads_available = MaxNumThreadsAvailable();
61 if (options->num_threads > num_threads_available) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080062 LOG(WARNING) << "Specified options.num_threads: " << options->num_threads
63 << " exceeds maximum available from the threading model Ceres "
64 << "was compiled with: " << num_threads_available
65 << ". Bounding to maximum number available.";
Austin Schuh70cc9552019-01-21 19:46:48 -080066 options->num_threads = num_threads_available;
67 }
68}
69
70void SetupCommonMinimizerOptions(PreprocessedProblem* pp) {
71 const Solver::Options& options = pp->options;
72 Program* program = pp->reduced_program.get();
73
74 // Assuming that the parameter blocks in the program have been
75 // reordered as needed, extract them into a contiguous vector.
76 pp->reduced_parameters.resize(program->NumParameters());
77 double* reduced_parameters = pp->reduced_parameters.data();
78 program->ParameterBlocksToStateVector(reduced_parameters);
79
80 Minimizer::Options& minimizer_options = pp->minimizer_options;
81 minimizer_options = Minimizer::Options(options);
82 minimizer_options.evaluator = pp->evaluator;
83
84 if (options.logging_type != SILENT) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080085 pp->logging_callback.reset(new LoggingCallback(
86 options.minimizer_type, options.minimizer_progress_to_stdout));
Austin Schuh70cc9552019-01-21 19:46:48 -080087 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
88 pp->logging_callback.get());
89 }
90
91 if (options.update_state_every_iteration) {
92 pp->state_updating_callback.reset(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080093 new StateUpdatingCallback(program, reduced_parameters));
Austin Schuh70cc9552019-01-21 19:46:48 -080094 // This must get pushed to the front of the callbacks so that it
95 // is run before any of the user callbacks.
96 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
97 pp->state_updating_callback.get());
98 }
99}
100
101} // namespace internal
102} // namespace ceres