Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 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 | #include "ceres/lapack.h" |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 32 | |
| 33 | #include "ceres/internal/port.h" |
| 34 | #include "ceres/linear_solver.h" |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 35 | #include "glog/logging.h" |
| 36 | |
| 37 | // C interface to the LAPACK Cholesky factorization and triangular solve. |
| 38 | extern "C" void dpotrf_(char* uplo, |
| 39 | int* n, |
| 40 | double* a, |
| 41 | int* lda, |
| 42 | int* info); |
| 43 | |
| 44 | extern "C" void dpotrs_(char* uplo, |
| 45 | int* n, |
| 46 | int* nrhs, |
| 47 | double* a, |
| 48 | int* lda, |
| 49 | double* b, |
| 50 | int* ldb, |
| 51 | int* info); |
| 52 | |
| 53 | extern "C" void dgels_(char* uplo, |
| 54 | int* m, |
| 55 | int* n, |
| 56 | int* nrhs, |
| 57 | double* a, |
| 58 | int* lda, |
| 59 | double* b, |
| 60 | int* ldb, |
| 61 | double* work, |
| 62 | int* lwork, |
| 63 | int* info); |
| 64 | |
| 65 | |
| 66 | namespace ceres { |
| 67 | namespace internal { |
| 68 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 69 | LinearSolverTerminationType LAPACK::SolveInPlaceUsingCholesky( |
| 70 | int num_rows, |
| 71 | const double* in_lhs, |
| 72 | double* rhs_and_solution, |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 73 | std::string* message) { |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 74 | #ifdef CERES_NO_LAPACK |
| 75 | LOG(FATAL) << "Ceres was built without a BLAS library."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 76 | return LINEAR_SOLVER_FATAL_ERROR; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 77 | #else |
| 78 | char uplo = 'L'; |
| 79 | int n = num_rows; |
| 80 | int info = 0; |
| 81 | int nrhs = 1; |
| 82 | double* lhs = const_cast<double*>(in_lhs); |
| 83 | |
| 84 | dpotrf_(&uplo, &n, lhs, &n, &info); |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 85 | if (info < 0) { |
| 86 | LOG(FATAL) << "Congratulations, you found a bug in Ceres." |
| 87 | << "Please report it." |
| 88 | << "LAPACK::dpotrf fatal error." |
| 89 | << "Argument: " << -info << " is invalid."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 90 | return LINEAR_SOLVER_FATAL_ERROR; |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | if (info > 0) { |
Sameer Agarwal | 3faac6a | 2013-11-28 07:13:26 -0800 | [diff] [blame] | 94 | *message = |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 95 | StringPrintf( |
| 96 | "LAPACK::dpotrf numerical failure. " |
Sameer Agarwal | d73acd0 | 2013-12-02 12:02:03 -0800 | [diff] [blame] | 97 | "The leading minor of order %d is not positive definite.", info); |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 98 | return LINEAR_SOLVER_FAILURE; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | dpotrs_(&uplo, &n, &nrhs, lhs, &n, rhs_and_solution, &n, &info); |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 102 | if (info < 0) { |
| 103 | LOG(FATAL) << "Congratulations, you found a bug in Ceres." |
| 104 | << "Please report it." |
| 105 | << "LAPACK::dpotrs fatal error." |
| 106 | << "Argument: " << -info << " is invalid."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 107 | return LINEAR_SOLVER_FATAL_ERROR; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Sameer Agarwal | 3faac6a | 2013-11-28 07:13:26 -0800 | [diff] [blame] | 110 | *message = "Success"; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 111 | return LINEAR_SOLVER_SUCCESS; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 112 | #endif |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 113 | } |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 114 | |
| 115 | int LAPACK::EstimateWorkSizeForQR(int num_rows, int num_cols) { |
| 116 | #ifdef CERES_NO_LAPACK |
| 117 | LOG(FATAL) << "Ceres was built without a LAPACK library."; |
| 118 | return -1; |
| 119 | #else |
| 120 | char trans = 'N'; |
| 121 | int nrhs = 1; |
| 122 | int lwork = -1; |
| 123 | double work; |
| 124 | int info = 0; |
Sameer Agarwal | d61b68a | 2013-08-16 17:02:56 -0700 | [diff] [blame] | 125 | dgels_(&trans, |
| 126 | &num_rows, |
| 127 | &num_cols, |
| 128 | &nrhs, |
| 129 | NULL, |
| 130 | &num_rows, |
| 131 | NULL, |
| 132 | &num_rows, |
| 133 | &work, |
| 134 | &lwork, |
| 135 | &info); |
| 136 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 137 | if (info < 0) { |
| 138 | LOG(FATAL) << "Congratulations, you found a bug in Ceres." |
| 139 | << "Please report it." |
| 140 | << "LAPACK::dgels fatal error." |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 141 | << "Argument: " << -info << " is invalid."; |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 142 | } |
Sameer Agarwal | 74727f5 | 2013-09-19 10:03:03 -0700 | [diff] [blame] | 143 | return static_cast<int>(work); |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 144 | #endif |
Sameer Agarwal | d61b68a | 2013-08-16 17:02:56 -0700 | [diff] [blame] | 145 | } |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 146 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 147 | LinearSolverTerminationType LAPACK::SolveInPlaceUsingQR( |
| 148 | int num_rows, |
| 149 | int num_cols, |
| 150 | const double* in_lhs, |
| 151 | int work_size, |
| 152 | double* work, |
| 153 | double* rhs_and_solution, |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 154 | std::string* message) { |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 155 | #ifdef CERES_NO_LAPACK |
| 156 | LOG(FATAL) << "Ceres was built without a LAPACK library."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 157 | return LINEAR_SOLVER_FATAL_ERROR; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 158 | #else |
| 159 | char trans = 'N'; |
| 160 | int m = num_rows; |
| 161 | int n = num_cols; |
| 162 | int nrhs = 1; |
| 163 | int lda = num_rows; |
| 164 | int ldb = num_rows; |
| 165 | int info = 0; |
| 166 | double* lhs = const_cast<double*>(in_lhs); |
| 167 | |
Sameer Agarwal | d61b68a | 2013-08-16 17:02:56 -0700 | [diff] [blame] | 168 | dgels_(&trans, |
| 169 | &m, |
| 170 | &n, |
| 171 | &nrhs, |
| 172 | lhs, |
| 173 | &lda, |
| 174 | rhs_and_solution, |
| 175 | &ldb, |
| 176 | work, |
| 177 | &work_size, |
| 178 | &info); |
| 179 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 180 | if (info < 0) { |
| 181 | LOG(FATAL) << "Congratulations, you found a bug in Ceres." |
| 182 | << "Please report it." |
| 183 | << "LAPACK::dgels fatal error." |
| 184 | << "Argument: " << -info << " is invalid."; |
| 185 | } |
| 186 | |
Sameer Agarwal | 3faac6a | 2013-11-28 07:13:26 -0800 | [diff] [blame] | 187 | *message = "Success."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 188 | return LINEAR_SOLVER_SUCCESS; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 189 | #endif |
Sameer Agarwal | d61b68a | 2013-08-16 17:02:56 -0700 | [diff] [blame] | 190 | } |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 191 | |
| 192 | } // namespace internal |
| 193 | } // namespace ceres |