blob: b74a75a9d0f8e2ace54417645b10354f9a252b43 [file] [log] [blame]
Austin Schuh3de38b02024-06-25 18:25:10 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2023 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: joydeepb@cs.utexas.edu (Joydeep Biswas)
30
31#include <string>
32
33#include "ceres/dense_cholesky.h"
34#include "ceres/internal/config.h"
35#include "ceres/internal/eigen.h"
36#include "glog/logging.h"
37#include "gtest/gtest.h"
38
39namespace ceres::internal {
40
41#ifndef CERES_NO_CUDA
42
43TEST(CUDADenseCholesky, InvalidOptionOnCreate) {
44 LinearSolver::Options options;
45 ContextImpl context;
46 options.context = &context;
47 std::string error;
48 EXPECT_TRUE(context.InitCuda(&error)) << error;
49 auto dense_cuda_solver = CUDADenseCholesky::Create(options);
50 EXPECT_EQ(dense_cuda_solver, nullptr);
51}
52
53// Tests the CUDA Cholesky solver with a simple 4x4 matrix.
54TEST(CUDADenseCholesky, Cholesky4x4Matrix) {
55 Eigen::Matrix4d A;
56 // clang-format off
57 A << 4, 12, -16, 0,
58 12, 37, -43, 0,
59 -16, -43, 98, 0,
60 0, 0, 0, 1;
61 // clang-format on
62
63 Vector b = Eigen::Vector4d::Ones();
64 LinearSolver::Options options;
65 ContextImpl context;
66 options.context = &context;
67 std::string error;
68 EXPECT_TRUE(context.InitCuda(&error)) << error;
69 options.dense_linear_algebra_library_type = CUDA;
70 auto dense_cuda_solver = CUDADenseCholesky::Create(options);
71 ASSERT_NE(dense_cuda_solver, nullptr);
72 std::string error_string;
73 ASSERT_EQ(dense_cuda_solver->Factorize(A.cols(), A.data(), &error_string),
74 LinearSolverTerminationType::SUCCESS);
75 Eigen::Vector4d x = Eigen::Vector4d::Zero();
76 ASSERT_EQ(dense_cuda_solver->Solve(b.data(), x.data(), &error_string),
77 LinearSolverTerminationType::SUCCESS);
78 static const double kEpsilon = std::numeric_limits<double>::epsilon() * 10;
79 const Eigen::Vector4d x_expected(113.75 / 3.0, -31.0 / 3.0, 5.0 / 3.0, 1.0);
80 EXPECT_NEAR((x[0] - x_expected[0]) / x_expected[0], 0.0, kEpsilon);
81 EXPECT_NEAR((x[1] - x_expected[1]) / x_expected[1], 0.0, kEpsilon);
82 EXPECT_NEAR((x[2] - x_expected[2]) / x_expected[2], 0.0, kEpsilon);
83 EXPECT_NEAR((x[3] - x_expected[3]) / x_expected[3], 0.0, kEpsilon);
84}
85
86TEST(CUDADenseCholesky, SingularMatrix) {
87 Eigen::Matrix3d A;
88 // clang-format off
89 A << 1, 0, 0,
90 0, 1, 0,
91 0, 0, 0;
92 // clang-format on
93
94 LinearSolver::Options options;
95 ContextImpl context;
96 options.context = &context;
97 std::string error;
98 EXPECT_TRUE(context.InitCuda(&error)) << error;
99 options.dense_linear_algebra_library_type = CUDA;
100 auto dense_cuda_solver = CUDADenseCholesky::Create(options);
101 ASSERT_NE(dense_cuda_solver, nullptr);
102 std::string error_string;
103 ASSERT_EQ(dense_cuda_solver->Factorize(A.cols(), A.data(), &error_string),
104 LinearSolverTerminationType::FAILURE);
105}
106
107TEST(CUDADenseCholesky, NegativeMatrix) {
108 Eigen::Matrix3d A;
109 // clang-format off
110 A << 1, 0, 0,
111 0, 1, 0,
112 0, 0, -1;
113 // clang-format on
114
115 LinearSolver::Options options;
116 ContextImpl context;
117 options.context = &context;
118 std::string error;
119 EXPECT_TRUE(context.InitCuda(&error)) << error;
120 options.dense_linear_algebra_library_type = CUDA;
121 auto dense_cuda_solver = CUDADenseCholesky::Create(options);
122 ASSERT_NE(dense_cuda_solver, nullptr);
123 std::string error_string;
124 ASSERT_EQ(dense_cuda_solver->Factorize(A.cols(), A.data(), &error_string),
125 LinearSolverTerminationType::FAILURE);
126}
127
128TEST(CUDADenseCholesky, MustFactorizeBeforeSolve) {
129 const Eigen::Vector3d b = Eigen::Vector3d::Ones();
130 LinearSolver::Options options;
131 ContextImpl context;
132 options.context = &context;
133 std::string error;
134 EXPECT_TRUE(context.InitCuda(&error)) << error;
135 options.dense_linear_algebra_library_type = CUDA;
136 auto dense_cuda_solver = CUDADenseCholesky::Create(options);
137 ASSERT_NE(dense_cuda_solver, nullptr);
138 std::string error_string;
139 ASSERT_EQ(dense_cuda_solver->Solve(b.data(), nullptr, &error_string),
140 LinearSolverTerminationType::FATAL_ERROR);
141}
142
143TEST(CUDADenseCholesky, Randomized1600x1600Tests) {
144 const int kNumCols = 1600;
145 using LhsType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
146 using RhsType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
147 using SolutionType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
148
149 LinearSolver::Options options;
150 ContextImpl context;
151 options.context = &context;
152 std::string error;
153 EXPECT_TRUE(context.InitCuda(&error)) << error;
154 options.dense_linear_algebra_library_type = ceres::CUDA;
155 std::unique_ptr<DenseCholesky> dense_cholesky =
156 CUDADenseCholesky::Create(options);
157
158 const int kNumTrials = 20;
159 for (int i = 0; i < kNumTrials; ++i) {
160 LhsType lhs = LhsType::Random(kNumCols, kNumCols);
161 lhs = lhs.transpose() * lhs;
162 lhs += 1e-3 * LhsType::Identity(kNumCols, kNumCols);
163 SolutionType x_expected = SolutionType::Random(kNumCols);
164 RhsType rhs = lhs * x_expected;
165 SolutionType x_computed = SolutionType::Zero(kNumCols);
166 // Sanity check the random matrix sizes.
167 EXPECT_EQ(lhs.rows(), kNumCols);
168 EXPECT_EQ(lhs.cols(), kNumCols);
169 EXPECT_EQ(rhs.rows(), kNumCols);
170 EXPECT_EQ(rhs.cols(), 1);
171 EXPECT_EQ(x_expected.rows(), kNumCols);
172 EXPECT_EQ(x_expected.cols(), 1);
173 EXPECT_EQ(x_computed.rows(), kNumCols);
174 EXPECT_EQ(x_computed.cols(), 1);
175 LinearSolver::Summary summary;
176 summary.termination_type = dense_cholesky->FactorAndSolve(
177 kNumCols, lhs.data(), rhs.data(), x_computed.data(), &summary.message);
178 ASSERT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
179 static const double kEpsilon = std::numeric_limits<double>::epsilon() * 3e5;
180 ASSERT_NEAR(
181 (x_computed - x_expected).norm() / x_expected.norm(), 0.0, kEpsilon);
182 }
183}
184
185TEST(CUDADenseCholeskyMixedPrecision, InvalidOptionsOnCreate) {
186 {
187 // Did not ask for CUDA, and did not ask for mixed precision.
188 LinearSolver::Options options;
189 ContextImpl context;
190 options.context = &context;
191 std::string error;
192 EXPECT_TRUE(context.InitCuda(&error)) << error;
193 auto solver = CUDADenseCholeskyMixedPrecision::Create(options);
194 ASSERT_EQ(solver, nullptr);
195 }
196 {
197 // Asked for CUDA, but did not ask for mixed precision.
198 LinearSolver::Options options;
199 ContextImpl context;
200 options.context = &context;
201 std::string error;
202 EXPECT_TRUE(context.InitCuda(&error)) << error;
203 options.dense_linear_algebra_library_type = ceres::CUDA;
204 auto solver = CUDADenseCholeskyMixedPrecision::Create(options);
205 ASSERT_EQ(solver, nullptr);
206 }
207}
208
209// Tests the CUDA Cholesky solver with a simple 4x4 matrix.
210TEST(CUDADenseCholeskyMixedPrecision, Cholesky4x4Matrix1Step) {
211 Eigen::Matrix4d A;
212 // clang-format off
213 // A common test Cholesky decomposition test matrix, see :
214 // https://en.wikipedia.org/w/index.php?title=Cholesky_decomposition&oldid=1080607368#Example
215 A << 4, 12, -16, 0,
216 12, 37, -43, 0,
217 -16, -43, 98, 0,
218 0, 0, 0, 1;
219 // clang-format on
220
221 const Eigen::Vector4d b = Eigen::Vector4d::Ones();
222 LinearSolver::Options options;
223 options.max_num_refinement_iterations = 0;
224 ContextImpl context;
225 options.context = &context;
226 std::string error;
227 EXPECT_TRUE(context.InitCuda(&error)) << error;
228 options.dense_linear_algebra_library_type = CUDA;
229 options.use_mixed_precision_solves = true;
230 auto solver = CUDADenseCholeskyMixedPrecision::Create(options);
231 ASSERT_NE(solver, nullptr);
232 std::string error_string;
233 ASSERT_EQ(solver->Factorize(A.cols(), A.data(), &error_string),
234 LinearSolverTerminationType::SUCCESS);
235 Eigen::Vector4d x = Eigen::Vector4d::Zero();
236 ASSERT_EQ(solver->Solve(b.data(), x.data(), &error_string),
237 LinearSolverTerminationType::SUCCESS);
238 // A single step of the mixed precision solver will be equivalent to solving
239 // in low precision (FP32). Hence the tolerance is defined w.r.t. FP32 epsilon
240 // instead of FP64 epsilon.
241 static const double kEpsilon = std::numeric_limits<float>::epsilon() * 10;
242 const Eigen::Vector4d x_expected(113.75 / 3.0, -31.0 / 3.0, 5.0 / 3.0, 1.0);
243 EXPECT_NEAR((x[0] - x_expected[0]) / x_expected[0], 0.0, kEpsilon);
244 EXPECT_NEAR((x[1] - x_expected[1]) / x_expected[1], 0.0, kEpsilon);
245 EXPECT_NEAR((x[2] - x_expected[2]) / x_expected[2], 0.0, kEpsilon);
246 EXPECT_NEAR((x[3] - x_expected[3]) / x_expected[3], 0.0, kEpsilon);
247}
248
249// Tests the CUDA Cholesky solver with a simple 4x4 matrix.
250TEST(CUDADenseCholeskyMixedPrecision, Cholesky4x4Matrix4Steps) {
251 Eigen::Matrix4d A;
252 // clang-format off
253 A << 4, 12, -16, 0,
254 12, 37, -43, 0,
255 -16, -43, 98, 0,
256 0, 0, 0, 1;
257 // clang-format on
258
259 const Eigen::Vector4d b = Eigen::Vector4d::Ones();
260 LinearSolver::Options options;
261 options.max_num_refinement_iterations = 3;
262 ContextImpl context;
263 options.context = &context;
264 std::string error;
265 EXPECT_TRUE(context.InitCuda(&error)) << error;
266 options.dense_linear_algebra_library_type = CUDA;
267 options.use_mixed_precision_solves = true;
268 auto solver = CUDADenseCholeskyMixedPrecision::Create(options);
269 ASSERT_NE(solver, nullptr);
270 std::string error_string;
271 ASSERT_EQ(solver->Factorize(A.cols(), A.data(), &error_string),
272 LinearSolverTerminationType::SUCCESS);
273 Eigen::Vector4d x = Eigen::Vector4d::Zero();
274 ASSERT_EQ(solver->Solve(b.data(), x.data(), &error_string),
275 LinearSolverTerminationType::SUCCESS);
276 // The error does not reduce beyond four iterations, and stagnates at this
277 // level of precision.
278 static const double kEpsilon = std::numeric_limits<double>::epsilon() * 100;
279 const Eigen::Vector4d x_expected(113.75 / 3.0, -31.0 / 3.0, 5.0 / 3.0, 1.0);
280 EXPECT_NEAR((x[0] - x_expected[0]) / x_expected[0], 0.0, kEpsilon);
281 EXPECT_NEAR((x[1] - x_expected[1]) / x_expected[1], 0.0, kEpsilon);
282 EXPECT_NEAR((x[2] - x_expected[2]) / x_expected[2], 0.0, kEpsilon);
283 EXPECT_NEAR((x[3] - x_expected[3]) / x_expected[3], 0.0, kEpsilon);
284}
285
286TEST(CUDADenseCholeskyMixedPrecision, Randomized1600x1600Tests) {
287 const int kNumCols = 1600;
288 using LhsType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
289 using RhsType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
290 using SolutionType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
291
292 LinearSolver::Options options;
293 ContextImpl context;
294 options.context = &context;
295 std::string error;
296 EXPECT_TRUE(context.InitCuda(&error)) << error;
297 options.dense_linear_algebra_library_type = ceres::CUDA;
298 options.use_mixed_precision_solves = true;
299 options.max_num_refinement_iterations = 20;
300 std::unique_ptr<CUDADenseCholeskyMixedPrecision> dense_cholesky =
301 CUDADenseCholeskyMixedPrecision::Create(options);
302
303 const int kNumTrials = 20;
304 for (int i = 0; i < kNumTrials; ++i) {
305 LhsType lhs = LhsType::Random(kNumCols, kNumCols);
306 lhs = lhs.transpose() * lhs;
307 lhs += 1e-3 * LhsType::Identity(kNumCols, kNumCols);
308 SolutionType x_expected = SolutionType::Random(kNumCols);
309 RhsType rhs = lhs * x_expected;
310 SolutionType x_computed = SolutionType::Zero(kNumCols);
311 // Sanity check the random matrix sizes.
312 EXPECT_EQ(lhs.rows(), kNumCols);
313 EXPECT_EQ(lhs.cols(), kNumCols);
314 EXPECT_EQ(rhs.rows(), kNumCols);
315 EXPECT_EQ(rhs.cols(), 1);
316 EXPECT_EQ(x_expected.rows(), kNumCols);
317 EXPECT_EQ(x_expected.cols(), 1);
318 EXPECT_EQ(x_computed.rows(), kNumCols);
319 EXPECT_EQ(x_computed.cols(), 1);
320 LinearSolver::Summary summary;
321 summary.termination_type = dense_cholesky->FactorAndSolve(
322 kNumCols, lhs.data(), rhs.data(), x_computed.data(), &summary.message);
323 ASSERT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
324 static const double kEpsilon = std::numeric_limits<double>::epsilon() * 1e6;
325 ASSERT_NEAR(
326 (x_computed - x_expected).norm() / x_expected.norm(), 0.0, kEpsilon);
327 }
328}
329
330#endif // CERES_NO_CUDA
331
332} // namespace ceres::internal