From 09886c6b3c138045ae3d90bf1f3ba96cfcd7759b Mon Sep 17 00:00:00 2001 From: Fritz Goebel Date: Wed, 24 Jul 2024 09:42:54 +0000 Subject: [PATCH] Add early return to CG --- core/solver/cg.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/solver/cg.cpp b/core/solver/cg.cpp index c512dc4313b..46a726edc3a 100644 --- a/core/solver/cg.cpp +++ b/core/solver/cg.cpp @@ -125,6 +125,22 @@ void Cg::apply_dense_impl(const VectorType* dense_b, this->get_system_matrix(), std::shared_ptr(dense_b, [](const LinOp*) {}), dense_x, r); + // If the initial guess is already a good enough solution, return + // immediately. + r->compute_conj_dot(r, rho, reduction_tmp); + bool all_stopped = + stop_criterion->update() + .num_iterations(0) + .residual(r) + .implicit_sq_residual_norm(rho) + .solution(dense_x) + .check(RelativeStoppingId, true, &stop_status, &one_changed); + this->template log( + this, dense_b, dense_x, 0, r, nullptr, rho, &stop_status, all_stopped); + if (all_stopped) { + return; + } + int iter = -1; /* Memory movement summary: * 18n * values + matrix/preconditioner storage