|
| 1 | +/*******************************<GINKGO LICENSE>****************************** |
| 2 | +Copyright (c) 2017-2023, the Ginkgo authors |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions |
| 7 | +are met: |
| 8 | +
|
| 9 | +1. Redistributions of source code must retain the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | +2. Redistributions in binary form must reproduce the above copyright |
| 13 | +notice, this list of conditions and the following disclaimer in the |
| 14 | +documentation and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +3. Neither the name of the copyright holder nor the names of its |
| 17 | +contributors may be used to endorse or promote products derived from |
| 18 | +this software without specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 21 | +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 | +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +******************************<GINKGO LICENSE>*******************************/ |
| 32 | + |
| 33 | +#ifndef GKO_CORE_SOLVER_RESIDUAL_UPDATE_HPP_ |
| 34 | +#define GKO_CORE_SOLVER_RESIDUAL_UPDATE_HPP_ |
| 35 | + |
| 36 | + |
| 37 | +#include <ginkgo/core/base/array.hpp> |
| 38 | +#include <ginkgo/core/matrix/dense.hpp> |
| 39 | +#include <ginkgo/core/stop/criterion.hpp> |
| 40 | + |
| 41 | + |
| 42 | +namespace gko { |
| 43 | +namespace solver { |
| 44 | + |
| 45 | + |
| 46 | +template <typename SolverType, typename VectorType, typename ScalarType, |
| 47 | + typename LogFunc> |
| 48 | +bool residual_update(SolverType* solver, int iter, const ScalarType* one_op, |
| 49 | + const ScalarType* neg_one_op, const VectorType* dense_b, |
| 50 | + VectorType* dense_x, VectorType* residual, |
| 51 | + const VectorType*& residual_ptr, |
| 52 | + std::unique_ptr<gko::stop::Criterion>& stop_criterion, |
| 53 | + uint8 relative_stopping_id, |
| 54 | + array<stopping_status>& stop_status, bool& one_changed, |
| 55 | + LogFunc log) |
| 56 | +{ |
| 57 | + if (iter == 0) { |
| 58 | + // In iter 0, the iteration and residual are updated. |
| 59 | + bool all_stopped = |
| 60 | + stop_criterion->update() |
| 61 | + .num_iterations(iter) |
| 62 | + .residual(residual_ptr) |
| 63 | + .solution(dense_x) |
| 64 | + .check(relative_stopping_id, true, &stop_status, &one_changed); |
| 65 | + log(solver, dense_b, dense_x, iter, residual_ptr, stop_status, |
| 66 | + all_stopped); |
| 67 | + return all_stopped; |
| 68 | + } else { |
| 69 | + // In the other iterations, the residual can be updated separately. |
| 70 | + bool all_stopped = |
| 71 | + stop_criterion->update() |
| 72 | + .num_iterations(iter) |
| 73 | + .solution(dense_x) |
| 74 | + // we have the residual check later |
| 75 | + .ignore_residual_check(true) |
| 76 | + .check(relative_stopping_id, false, &stop_status, &one_changed); |
| 77 | + if (all_stopped) { |
| 78 | + log(solver, dense_b, dense_x, iter, nullptr, stop_status, |
| 79 | + all_stopped); |
| 80 | + return all_stopped; |
| 81 | + } |
| 82 | + residual_ptr = residual; |
| 83 | + // residual = b - A * x |
| 84 | + residual->copy_from(dense_b); |
| 85 | + solver->get_system_matrix()->apply(neg_one_op, dense_x, one_op, |
| 86 | + residual); |
| 87 | + all_stopped = |
| 88 | + stop_criterion->update() |
| 89 | + .num_iterations(iter) |
| 90 | + .residual(residual_ptr) |
| 91 | + .solution(dense_x) |
| 92 | + .check(relative_stopping_id, true, &stop_status, &one_changed); |
| 93 | + log(solver, dense_b, dense_x, iter, residual_ptr, stop_status, |
| 94 | + all_stopped); |
| 95 | + return all_stopped; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +} // namespace solver |
| 101 | +} // namespace gko |
| 102 | + |
| 103 | +#endif // GKO_CORE_SOLVER_RESIDUAL_UPDATE_HPP_ |
0 commit comments