Skip to content

Commit 34bc634

Browse files
MarcelKochthoasmvenkovic
committed
review updates:
- documentation - missing tests - refactor Co-authored-by: Thomas Grützmacher <[email protected]> Co-authored-by: nvenko <[email protected]> Signed-off-by: Marcel Koch <[email protected]>
1 parent 80293c5 commit 34bc634

File tree

7 files changed

+75
-31
lines changed

7 files changed

+75
-31
lines changed

common/unified/solver/minres_kernels.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ namespace minres {
2121
namespace detail {
2222

2323

24-
template <typename T, typename U>
25-
GKO_INLINE GKO_ATTRIBUTES void swap(T& a, U& b)
24+
template <typename T>
25+
GKO_INLINE GKO_ATTRIBUTES void swap(T& a, T& b)
2626
{
27-
U tmp{b};
27+
T tmp{b};
2828
b = a;
2929
a = tmp;
3030
}

core/solver/minres.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

5+
#include <utility>
6+
57
#include <ginkgo/core/base/exception.hpp>
68
#include <ginkgo/core/base/exception_helpers.hpp>
79
#include <ginkgo/core/base/executor.hpp>
@@ -25,6 +27,7 @@ GKO_REGISTER_OPERATION(initialize, minres::initialize);
2527
GKO_REGISTER_OPERATION(step_1, minres::step_1);
2628
GKO_REGISTER_OPERATION(step_2, minres::step_2);
2729

30+
2831
} // anonymous namespace
2932
} // namespace minres
3033

core/solver/minres_kernels.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace gko {
2121
namespace kernels {
22-
namespace cg {
22+
namespace minres {
2323

2424

2525
#define GKO_DECLARE_MINRES_INITIALIZE_KERNEL(_type) \
@@ -67,7 +67,7 @@ namespace cg {
6767
GKO_DECLARE_MINRES_STEP_2_KERNEL(ValueType)
6868

6969

70-
} // namespace cg
70+
} // namespace minres
7171

7272

7373
GKO_DECLARE_FOR_ALL_EXECUTOR_NAMESPACES(minres, GKO_DECLARE_ALL_AS_TEMPLATES);

include/ginkgo/core/solver/minres.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ namespace solver {
3535
* use of data locality. The inner operations in one iteration of Minres are
3636
* merged into 2 separate steps.
3737
*
38+
* For more details see Anne Grennbaum's 'Iterative Methods
39+
* for Solving Linear Systems' (DOI: 10.1137/1.9781611970937), and Sou-Cheng
40+
* (Terrya) Choi's 'ITERATIVE METHODS FOR SINGULAR LINEAR EQUATIONS AND
41+
* LEAST-SQUARES PROBLEMS'.
42+
*
3843
* @tparam ValueType precision of matrix elements
3944
*
4045
* @ingroup solvers

reference/solver/minres_kernels.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "core/solver/minres_kernels.hpp"
66

7+
#include <utility>
8+
79
#include <ginkgo/core/base/array.hpp>
810
#include <ginkgo/core/base/exception_helpers.hpp>
911
#include <ginkgo/core/base/math.hpp>

reference/test/solver/minres_kernels.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
#include "core/test/utils.hpp"
2020

21+
2122
namespace {
2223

24+
2325
template <typename T>
2426
class Minres : public ::testing::Test {
2527
protected:
@@ -178,6 +180,32 @@ TYPED_TEST(Minres, KernelInitialize)
178180
}
179181

180182

183+
TYPED_TEST(Minres, KernelInitializeWithSafeDivide)
184+
{
185+
using Mtx = typename TestFixture::Mtx;
186+
using vt = typename TestFixture::value_type;
187+
this->small_r =
188+
gko::initialize<Mtx>(I<I<vt>>({{1, 2}, {3, 4}}), this->exec);
189+
this->small_z = gko::initialize<Mtx>(I<I<vt>>{{4, 3}, {2, 1}}, this->exec);
190+
auto zero = gko::zero<vt>();
191+
this->beta = gko::initialize<Mtx>(I<I<vt>>{{zero, zero}}, this->exec);
192+
this->small_q->fill(1);
193+
194+
gko::kernels::reference::minres::initialize(
195+
this->exec, this->small_r.get(), this->small_z.get(),
196+
this->small_p.get(), this->small_p_prev.get(), this->small_q.get(),
197+
this->small_q_prev.get(), this->small_v.get(), this->beta.get(),
198+
this->gamma.get(), this->delta.get(), this->cos_prev.get(),
199+
this->cos.get(), this->sin_prev.get(), this->sin.get(),
200+
this->eta_next.get(), this->eta.get(), &this->small_stop);
201+
202+
GKO_ASSERT_MTX_NEAR(this->small_q, l({{0.0, 2. / 5}, {0.0, 4. / 5}}),
203+
r<vt>::value);
204+
GKO_ASSERT_MTX_NEAR(this->small_z, l({{0.0, 3. / 5}, {0.0, 1. / 5}}),
205+
r<vt>::value);
206+
}
207+
208+
181209
TYPED_TEST(Minres, KernelStep1)
182210
{
183211
using Mtx = typename TestFixture::Mtx;
@@ -273,6 +301,36 @@ TYPED_TEST(Minres, KernelStep2)
273301
}
274302

275303

304+
TYPED_TEST(Minres, KernelStep2WithSafeDivide)
305+
{
306+
using Mtx = typename TestFixture::Mtx;
307+
using vt = typename TestFixture::value_type;
308+
this->small_q = gko::initialize<Mtx>(I<I<vt>>{{4, 9}, {7, 11}}, this->exec);
309+
this->small_p = gko::initialize<Mtx>(I<I<vt>>{{1, 2}, {3, 4}}, this->exec);
310+
this->small_z = gko::initialize<Mtx>(I<I<vt>>{{6, 1}, {7, 3}}, this->exec);
311+
auto zero = gko::zero<vt>();
312+
this->alpha = gko::initialize<Mtx>(I<I<vt>>{{zero, zero}}, this->exec);
313+
this->beta = gko::initialize<Mtx>(I<I<vt>>{{zero, zero}}, this->exec);
314+
auto old_small_q = gko::clone(this->small_q);
315+
auto old_small_v = gko::clone(this->small_v);
316+
auto old_small_q_scaled = gko::clone(this->small_v);
317+
auto old_small_z_tilde_scaled = gko::clone(this->small_z_tilde);
318+
auto old_small_v_scaled = gko::clone(this->small_q);
319+
320+
gko::kernels::reference::minres::step_2(
321+
this->exec, this->small_x.get(), this->small_p.get(),
322+
this->small_p_prev.get(), this->small_z.get(),
323+
this->small_z_tilde.get(), this->small_q.get(),
324+
this->small_q_prev.get(), this->small_v.get(), this->alpha.get(),
325+
this->beta.get(), this->gamma.get(), this->delta.get(), this->cos.get(),
326+
this->eta.get(), &this->small_stop);
327+
328+
GKO_ASSERT_MTX_NEAR(this->small_q, l({{0.0, 0.0}, {0.0, 0.0}}), 0.);
329+
GKO_ASSERT_MTX_NEAR(this->small_z, l({{0.0, 0.0}, {0.0, 0.0}}), 0.);
330+
GKO_ASSERT_MTX_NEAR(this->small_p, l({{0.0, 0.0}, {0.0, 0.0}}), 0.);
331+
}
332+
333+
276334
TYPED_TEST(Minres, SolvesSystem)
277335
{
278336
using Mtx = typename TestFixture::Mtx;

test/solver/minres_kernels.cpp

+2-26
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,11 @@
2222

2323
namespace {
2424

25-
class Minres : public ::testing::Test {
25+
class Minres : public CommonTestFixture {
2626
protected:
27-
#if GINKGO_COMMON_SINGLE_MODE
28-
using value_type = float;
29-
#else
30-
using value_type = double;
31-
#endif
3227
using Mtx = gko::matrix::Dense<value_type>;
3328
using Solver = gko::solver::Minres<value_type>;
3429

35-
Minres() : rand_engine(42) {}
36-
37-
void SetUp()
38-
{
39-
ref = gko::ReferenceExecutor::create();
40-
init_executor(ref, exec);
41-
}
42-
43-
void TearDown()
44-
{
45-
if (exec != nullptr) {
46-
ASSERT_NO_THROW(exec->synchronize());
47-
}
48-
}
49-
5030
std::unique_ptr<Mtx> gen_mtx(gko::size_type num_rows,
5131
gko::size_type num_cols, gko::size_type stride,
5232
bool make_hermitian)
@@ -126,11 +106,7 @@ class Minres : public ::testing::Test {
126106
exec, *stop_status);
127107
}
128108

129-
130-
std::shared_ptr<gko::ReferenceExecutor> ref;
131-
std::shared_ptr<gko::EXEC_TYPE> exec;
132-
133-
std::default_random_engine rand_engine;
109+
std::default_random_engine rand_engine{42};
134110

135111
std::unique_ptr<Mtx> x;
136112
std::unique_ptr<Mtx> b;

0 commit comments

Comments
 (0)