|
| 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 | +#include <random> |
| 34 | + |
| 35 | + |
| 36 | +#include <gtest/gtest.h> |
| 37 | + |
| 38 | + |
| 39 | +#include <ginkgo/core/base/exception.hpp> |
| 40 | +#include <ginkgo/core/base/executor.hpp> |
| 41 | +#include <ginkgo/core/matrix/dense.hpp> |
| 42 | +#include <ginkgo/core/solver/chebyshev.hpp> |
| 43 | +#include <ginkgo/core/solver/gmres.hpp> |
| 44 | +#include <ginkgo/core/stop/combined.hpp> |
| 45 | +#include <ginkgo/core/stop/iteration.hpp> |
| 46 | + |
| 47 | + |
| 48 | +#include "core/test/utils.hpp" |
| 49 | +#include "test/utils/executor.hpp" |
| 50 | + |
| 51 | + |
| 52 | +class Chebyshev : public CommonTestFixture { |
| 53 | +protected: |
| 54 | + using Mtx = gko::matrix::Dense<value_type>; |
| 55 | + |
| 56 | + Chebyshev() : rand_engine(30) {} |
| 57 | + |
| 58 | + std::unique_ptr<Mtx> gen_mtx(gko::size_type num_rows, |
| 59 | + gko::size_type num_cols, gko::size_type stride) |
| 60 | + { |
| 61 | + auto tmp_mtx = gko::test::generate_random_matrix<Mtx>( |
| 62 | + num_rows, num_cols, |
| 63 | + std::uniform_int_distribution<>(num_cols, num_cols), |
| 64 | + std::normal_distribution<value_type>(-1.0, 1.0), rand_engine, ref); |
| 65 | + auto result = Mtx::create(ref, gko::dim<2>{num_rows, num_cols}, stride); |
| 66 | + result->copy_from(tmp_mtx); |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + std::default_random_engine rand_engine; |
| 71 | +}; |
| 72 | + |
| 73 | + |
| 74 | +TEST_F(Chebyshev, ApplyIsEquivalentToRef) |
| 75 | +{ |
| 76 | + auto mtx = gen_mtx(50, 50, 52); |
| 77 | + auto x = gen_mtx(50, 3, 8); |
| 78 | + auto b = gen_mtx(50, 3, 5); |
| 79 | + auto d_mtx = clone(exec, mtx); |
| 80 | + auto d_x = clone(exec, x); |
| 81 | + auto d_b = clone(exec, b); |
| 82 | + // Forget about accuracy - Chebyshev is not going to converge for a random |
| 83 | + // matrix, just check that a couple of iterations gives the same result on |
| 84 | + // both executors |
| 85 | + auto chebyshev_factory = |
| 86 | + gko::solver::Chebyshev<value_type>::build() |
| 87 | + .with_criteria( |
| 88 | + gko::stop::Iteration::build().with_max_iters(2u).on(ref)) |
| 89 | + .on(ref); |
| 90 | + auto d_chebyshev_factory = |
| 91 | + gko::solver::Chebyshev<value_type>::build() |
| 92 | + .with_criteria( |
| 93 | + gko::stop::Iteration::build().with_max_iters(2u).on(exec)) |
| 94 | + .on(exec); |
| 95 | + auto solver = chebyshev_factory->generate(std::move(mtx)); |
| 96 | + auto d_solver = d_chebyshev_factory->generate(std::move(d_mtx)); |
| 97 | + |
| 98 | + solver->apply(b, x); |
| 99 | + d_solver->apply(d_b, d_x); |
| 100 | + |
| 101 | + GKO_ASSERT_MTX_NEAR(d_x, x, r<value_type>::value); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +TEST_F(Chebyshev, ApplyWithIterativeInnerSolverIsEquivalentToRef) |
| 106 | +{ |
| 107 | + auto mtx = gen_mtx(50, 50, 54); |
| 108 | + auto x = gen_mtx(50, 3, 6); |
| 109 | + auto b = gen_mtx(50, 3, 10); |
| 110 | + auto d_mtx = clone(exec, mtx); |
| 111 | + auto d_x = clone(exec, x); |
| 112 | + auto d_b = clone(exec, b); |
| 113 | + |
| 114 | + auto chebyshev_factory = |
| 115 | + gko::solver::Chebyshev<value_type>::build() |
| 116 | + .with_preconditioner( |
| 117 | + gko::solver::Gmres<value_type>::build() |
| 118 | + .with_criteria( |
| 119 | + gko::stop::Iteration::build().with_max_iters(1u).on( |
| 120 | + ref)) |
| 121 | + .on(ref)) |
| 122 | + .with_criteria( |
| 123 | + gko::stop::Iteration::build().with_max_iters(2u).on(ref)) |
| 124 | + .on(ref); |
| 125 | + auto d_chebyshev_factory = |
| 126 | + gko::solver::Chebyshev<value_type>::build() |
| 127 | + .with_preconditioner( |
| 128 | + gko::solver::Gmres<value_type>::build() |
| 129 | + .with_criteria( |
| 130 | + gko::stop::Iteration::build().with_max_iters(1u).on( |
| 131 | + exec)) |
| 132 | + .on(exec)) |
| 133 | + .with_criteria( |
| 134 | + gko::stop::Iteration::build().with_max_iters(2u).on(exec)) |
| 135 | + .on(exec); |
| 136 | + auto solver = chebyshev_factory->generate(std::move(mtx)); |
| 137 | + auto d_solver = d_chebyshev_factory->generate(std::move(d_mtx)); |
| 138 | + |
| 139 | + solver->apply(b, x); |
| 140 | + d_solver->apply(d_b, d_x); |
| 141 | + |
| 142 | + // Note: r<value_type>::value * 300 instead of r<value_type>::value, as |
| 143 | + // the difference in the inner gmres iteration gets amplified by the |
| 144 | + // difference in IR. |
| 145 | + GKO_ASSERT_MTX_NEAR(d_x, x, r<value_type>::value * 300); |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +TEST_F(Chebyshev, ApplyWithGivenInitialGuessModeIsEquivalentToRef) |
| 150 | +{ |
| 151 | + using initial_guess_mode = gko::solver::initial_guess_mode; |
| 152 | + auto mtx = gko::share(gen_mtx(50, 50, 52)); |
| 153 | + auto b = gen_mtx(50, 3, 7); |
| 154 | + auto d_mtx = gko::share(clone(exec, mtx)); |
| 155 | + auto d_b = clone(exec, b); |
| 156 | + for (auto guess : {initial_guess_mode::provided, initial_guess_mode::rhs, |
| 157 | + initial_guess_mode::zero}) { |
| 158 | + auto x = gen_mtx(50, 3, 4); |
| 159 | + auto d_x = clone(exec, x); |
| 160 | + auto chebyshev_factory = |
| 161 | + gko::solver::Chebyshev<value_type>::build() |
| 162 | + .with_criteria( |
| 163 | + gko::stop::Iteration::build().with_max_iters(2u).on(ref)) |
| 164 | + .with_default_initial_guess(guess) |
| 165 | + .on(ref); |
| 166 | + auto d_chebyshev_factory = |
| 167 | + gko::solver::Chebyshev<value_type>::build() |
| 168 | + .with_criteria( |
| 169 | + gko::stop::Iteration::build().with_max_iters(2u).on(exec)) |
| 170 | + .with_default_initial_guess(guess) |
| 171 | + .on(exec); |
| 172 | + auto solver = chebyshev_factory->generate(mtx); |
| 173 | + auto d_solver = d_chebyshev_factory->generate(d_mtx); |
| 174 | + |
| 175 | + solver->apply(b, x); |
| 176 | + d_solver->apply(d_b, d_x); |
| 177 | + |
| 178 | + GKO_ASSERT_MTX_NEAR(d_x, x, r<value_type>::value); |
| 179 | + } |
| 180 | +} |
0 commit comments