Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #ifndef CERES_INTERNAL_REORDER_PROGRAM_H_ |
| 32 | #define CERES_INTERNAL_REORDER_PROGRAM_H_ |
| 33 | |
| 34 | #include <string> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/internal/port.h" |
| 37 | #include "ceres/parameter_block_ordering.h" |
| 38 | #include "ceres/problem_impl.h" |
| 39 | #include "ceres/types.h" |
| 40 | |
| 41 | namespace ceres { |
| 42 | namespace internal { |
| 43 | |
| 44 | class Program; |
| 45 | |
| 46 | // Reorder the parameter blocks in program using the ordering |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 47 | CERES_EXPORT_INTERNAL bool ApplyOrdering( |
| 48 | const ProblemImpl::ParameterMap& parameter_map, |
| 49 | const ParameterBlockOrdering& ordering, |
| 50 | Program* program, |
| 51 | std::string* error); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 52 | |
| 53 | // Reorder the residuals for program, if necessary, so that the residuals |
| 54 | // involving each E block occur together. This is a necessary condition for the |
| 55 | // Schur eliminator, which works on these "row blocks" in the jacobian. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 56 | CERES_EXPORT_INTERNAL bool LexicographicallyOrderResidualBlocks( |
| 57 | int size_of_first_elimination_group, Program* program, std::string* error); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 58 | |
| 59 | // Schur type solvers require that all parameter blocks eliminated |
| 60 | // by the Schur eliminator occur before others and the residuals be |
| 61 | // sorted in lexicographic order of their parameter blocks. |
| 62 | // |
| 63 | // If the parameter_block_ordering only contains one elimination |
| 64 | // group then a maximal independent set is computed and used as the |
| 65 | // first elimination group, otherwise the user's ordering is used. |
| 66 | // |
| 67 | // If the linear solver type is SPARSE_SCHUR and support for |
| 68 | // constrained fill-reducing ordering is available in the sparse |
| 69 | // linear algebra library (SuiteSparse version >= 4.2.0) then |
| 70 | // columns of the schur complement matrix are ordered to reduce the |
| 71 | // fill-in the Cholesky factorization. |
| 72 | // |
| 73 | // Upon return, ordering contains the parameter block ordering that |
| 74 | // was used to order the program. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 75 | CERES_EXPORT_INTERNAL bool ReorderProgramForSchurTypeLinearSolver( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | LinearSolverType linear_solver_type, |
| 77 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type, |
| 78 | const ProblemImpl::ParameterMap& parameter_map, |
| 79 | ParameterBlockOrdering* parameter_block_ordering, |
| 80 | Program* program, |
| 81 | std::string* error); |
| 82 | |
| 83 | // Sparse cholesky factorization routines when doing the sparse |
| 84 | // cholesky factorization of the Jacobian matrix, reorders its |
| 85 | // columns to reduce the fill-in. Compute this permutation and |
| 86 | // re-order the parameter blocks. |
| 87 | // |
| 88 | // When using SuiteSparse, if the parameter_block_ordering contains |
| 89 | // more than one elimination group and support for constrained |
| 90 | // fill-reducing ordering is available in the sparse linear algebra |
| 91 | // library (SuiteSparse version >= 4.2.0) then the fill reducing |
| 92 | // ordering will take it into account, otherwise it will be ignored. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 93 | CERES_EXPORT_INTERNAL bool ReorderProgramForSparseCholesky( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 94 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type, |
| 95 | const ParameterBlockOrdering& parameter_block_ordering, |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 96 | int start_row_block, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 97 | Program* program, |
| 98 | std::string* error); |
| 99 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 100 | // Reorder the residual blocks in the program so that all the residual |
| 101 | // blocks in bottom_residual_blocks are at the bottom. The return |
| 102 | // value is the number of residual blocks in the program in "top" part |
| 103 | // of the Program, i.e., the ones not included in |
| 104 | // bottom_residual_blocks. |
| 105 | // |
| 106 | // This number can be different from program->NumResidualBlocks() - |
| 107 | // bottom_residual_blocks.size() because we allow |
| 108 | // bottom_residual_blocks to contain residual blocks not present in |
| 109 | // the Program. |
| 110 | CERES_EXPORT_INTERNAL int ReorderResidualBlocksByPartition( |
| 111 | const std::unordered_set<ResidualBlockId>& bottom_residual_blocks, |
| 112 | Program* program); |
| 113 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 114 | } // namespace internal |
| 115 | } // namespace ceres |
| 116 | |
| 117 | #endif // CERES_INTERNAL_REORDER_PROGRAM_ |