Skip to content

Commit b6bfe6e

Browse files
upsjMarcelKochyhmtsai
committed
review updates
- rename functions to match configuration keys - add documentation - add missing tests Co-authored-by: Marcel Koch <[email protected]> Co-authored-by: Yu-Hsiang M. Tsai <[email protected]>
1 parent e37997c commit b6bfe6e

File tree

11 files changed

+130
-37
lines changed

11 files changed

+130
-37
lines changed

core/stop/iteration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool Iteration::check_impl(uint8 stoppingId, bool setFinalized,
2424
}
2525

2626

27-
deferred_factory_parameter<Iteration::Factory> iteration(size_type count)
27+
deferred_factory_parameter<Iteration::Factory> max_iters(size_type count)
2828
{
2929
return Iteration::build().with_max_iters(count);
3030
}

core/stop/residual_norm.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,25 @@ class ResidualNormFactory
311311
};
312312

313313

314-
deferred_factory_parameter<CriterionFactory> abs_residual_norm(double tolerance)
314+
deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
315+
double tolerance)
315316
{
316317
return residual_norm_factory_parameters{}
317318
.with_threshold(tolerance)
318319
.with_baseline(mode::absolute);
319320
}
320321

321322

322-
deferred_factory_parameter<CriterionFactory> rel_residual_norm(double tolerance)
323+
deferred_factory_parameter<CriterionFactory> relative_residual_norm(
324+
double tolerance)
323325
{
324326
return residual_norm_factory_parameters{}
325327
.with_threshold(tolerance)
326328
.with_baseline(mode::rhs_norm);
327329
}
328330

329331

330-
deferred_factory_parameter<CriterionFactory> residual_norm_reduction(
332+
deferred_factory_parameter<CriterionFactory> initial_residual_norm(
331333
double tolerance)
332334
{
333335
return residual_norm_factory_parameters{}
@@ -414,7 +416,7 @@ class ImplicitResidualNormFactory
414416
};
415417

416418

417-
deferred_factory_parameter<CriterionFactory> implicit_abs_residual_norm(
419+
deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
418420
double tolerance)
419421
{
420422
return implicit_residual_norm_factory_parameters{}
@@ -423,7 +425,7 @@ deferred_factory_parameter<CriterionFactory> implicit_abs_residual_norm(
423425
}
424426

425427

426-
deferred_factory_parameter<CriterionFactory> implicit_rel_residual_norm(
428+
deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
427429
double tolerance)
428430
{
429431
return implicit_residual_norm_factory_parameters{}
@@ -432,7 +434,7 @@ deferred_factory_parameter<CriterionFactory> implicit_rel_residual_norm(
432434
}
433435

434436

435-
deferred_factory_parameter<CriterionFactory> implicit_residual_norm_reduction(
437+
deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
436438
double tolerance)
437439
{
438440
return implicit_residual_norm_factory_parameters{}

core/stop/time.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "ginkgo/core/stop/time.hpp"
66

7+
#include <chrono>
8+
79
#include <ginkgo/core/base/abstract_factory.hpp>
810

911

@@ -24,17 +26,10 @@ bool Time::check_impl(uint8 stoppingId, bool setFinalized,
2426
}
2527

2628

27-
deferred_factory_parameter<Time::Factory> time_sec(double time)
28-
{
29-
return Time::build().with_time_limit(
30-
std::chrono::nanoseconds{static_cast<long>(time * 1e9)});
31-
}
32-
33-
34-
deferred_factory_parameter<Time::Factory> time_ms(double time)
29+
deferred_factory_parameter<Time::Factory> time_limit(
30+
std::chrono::nanoseconds time)
3531
{
36-
return Time::build().with_time_limit(
37-
std::chrono::nanoseconds{static_cast<long>(time * 1e6)});
32+
return Time::build().with_time_limit(time);
3833
}
3934

4035

include/ginkgo/core/stop/iteration.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,27 @@ class Iteration : public EnablePolymorphicObject<Iteration, Criterion> {
6060
};
6161

6262

63-
deferred_factory_parameter<Iteration::Factory> iteration(size_type count);
63+
/**
64+
* Creates the precursor to an Iteration stopping criterion factory, to be used
65+
* in conjunction with `.with_criteria(...)` function calls when building a
66+
* solver factory. This stopping criterion will stop the iteration after `count`
67+
* iterations of the solver have finished.
68+
*
69+
* Full usage example: Stop after 100 iterations or when the relative residual
70+
* norm is below $10^{-10}$, whichever happens first.
71+
* ```cpp
72+
* auto factory = gko::solver::Cg<double>::build()
73+
* .with_criteria(
74+
* gko::stop::max_iters(100),
75+
* gko::stop::relative_residual_norm(1e-10))
76+
* .on(exec);
77+
* ```
78+
*
79+
* @param count the number of iterations after which to stop
80+
* @return a deferred_factory_parameter that can be passed to the
81+
* `with_criteria` function when building a solver.
82+
*/
83+
deferred_factory_parameter<Iteration::Factory> max_iters(size_type count);
6484

6585

6686
} // namespace stop

include/ginkgo/core/stop/residual_norm.hpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,58 @@ class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
223223
};
224224

225225

226-
deferred_factory_parameter<CriterionFactory> abs_residual_norm(
226+
/**
227+
* Creates the precursor to a ResidualNorm stopping criterion factory, to be
228+
* used in conjunction with `.with_criteria(...)` function calls when building a
229+
* solver factory. This stopping criterion will stop the iteration after the
230+
* residual norm has decreased below the specified value or by the specified
231+
* amount.
232+
*
233+
* Full usage example: Stop after 100 iterations or when the absolute residual
234+
* norm is below $10^{-10}$, whichever happens first.
235+
* ```cpp
236+
* auto factory = gko::solver::Cg<double>::build()
237+
* .with_criteria(
238+
* gko::stop::max_iters(100),
239+
* gko::stop::absolute_residual_norm(1e-10))
240+
* .on(exec);
241+
* ```
242+
*
243+
* @param tolerance the value the residual norm needs to be below.
244+
* With residual $r$, initial guess $x_0$, right-hand side $b$, matrix $A$,
245+
* `absolute` means the exact value of the norm $||r||$,
246+
* `relative` means the norm relative to the right-hand side $||r||/||b||$,
247+
* `initial` means the norm relative to the initial residual
248+
* $||r||/||b - A x_0||$.
249+
* An implicit stopping criterion is only available with some solvers, and
250+
* refers to either the energy norm $||r||_A$ in short-recurrence solvers
251+
* like Cg or the euclidian norm $||r||$ in solvers like GMRES.
252+
* Implicit residual norms are cheaper to compute, but may be less precise
253+
* due to accumulating rounding errors.
254+
* @return a deferred_factory_parameter that can be passed to the
255+
* `with_criteria` function when building a solver.
256+
*/
257+
deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
227258
double tolerance);
228259

229-
deferred_factory_parameter<CriterionFactory> rel_residual_norm(
260+
/** @copydoc absolute_residual_norm */
261+
deferred_factory_parameter<CriterionFactory> relative_residual_norm(
230262
double tolerance);
231263

232-
deferred_factory_parameter<CriterionFactory> residual_norm_reduction(
264+
/** @copydoc absolute_residual_norm */
265+
deferred_factory_parameter<CriterionFactory> initial_residual_norm(
233266
double tolerance);
234267

235-
deferred_factory_parameter<CriterionFactory> implicit_abs_residual_norm(
268+
/** @copydoc absolute_residual_norm */
269+
deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
236270
double tolerance);
237271

238-
deferred_factory_parameter<CriterionFactory> implicit_rel_residual_norm(
272+
/** @copydoc absolute_residual_norm */
273+
deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
239274
double tolerance);
240275

241-
deferred_factory_parameter<CriterionFactory> implicit_residual_norm_reduction(
276+
/** @copydoc absolute_residual_norm */
277+
deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
242278
double tolerance);
243279

244280

include/ginkgo/core/stop/time.hpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,31 @@ class Time : public EnablePolymorphicObject<Time, Criterion> {
6969
};
7070

7171

72-
deferred_factory_parameter<Time::Factory> time_sec(double time);
73-
deferred_factory_parameter<Time::Factory> time_ms(double time);
72+
/**
73+
* Creates the precursor to a Time stopping criterion factory, to be used
74+
* in conjunction with `.with_criteria(...)` function calls when building a
75+
* solver factory. This stopping criterion will stop the iteration after the
76+
* specified amount of time since the start of the solver run has elapsed.
77+
*
78+
* Full usage example: Stop after 1 second or when the relative residual norm is
79+
* below $10^{-10}$, whichever happens first.
80+
* ```cpp
81+
* auto factory = gko::solver::Cg<double>::build()
82+
* .with_criteria(
83+
* gko::stop::time_limit(std::chrono::seconds(1)),
84+
* gko::stop::relative_residual_norm(1e-10))
85+
* .on(exec);
86+
* ```
87+
*
88+
* @param duration the time limit after which to stop the iteration.
89+
* Thanks to std::chrono's converting constructors, you can
90+
* specify any time units, and they will be converted to
91+
* nanoseconds automatically.
92+
* @return a deferred_factory_parameter that can be passed to the
93+
* `with_criteria` function when building a solver.
94+
*/
95+
deferred_factory_parameter<Time::Factory> time_limit(
96+
std::chrono::nanoseconds duration);
7497

7598

7699
} // namespace stop

reference/test/stop/iteration.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class Iteration : public ::testing::Test {
2727
};
2828

2929

30+
TEST_F(Iteration, NewInterface)
31+
{
32+
auto factory = gko::stop::max_iters(test_iterations).on(exec_);
33+
ASSERT_EQ(factory->get_parameters().max_iters, test_iterations);
34+
}
35+
36+
3037
TEST_F(Iteration, WaitsTillIteration)
3138
{
3239
bool one_changed{};

reference/test/stop/residual_norm_kernels.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ TYPED_TEST(ResidualNorm, SimplifiedInterface)
518518
auto initial_guess = gko::initialize<Mtx>({1000.0}, this->exec_);
519519
std::shared_ptr<gko::LinOp> rhs = gko::initialize<Mtx>({10.0}, this->exec_);
520520

521-
auto factory_abs = gko::stop::abs_residual_norm(0.5).on(this->exec_);
522-
auto factory_rel = gko::stop::rel_residual_norm(0.5).on(this->exec_);
523-
auto factory_red = gko::stop::residual_norm_reduction(0.5).on(this->exec_);
521+
auto factory_abs = gko::stop::absolute_residual_norm(0.5).on(this->exec_);
522+
auto factory_rel = gko::stop::relative_residual_norm(0.5).on(this->exec_);
523+
auto factory_red = gko::stop::initial_residual_norm(0.5).on(this->exec_);
524524

525525
auto crit_abs =
526526
gko::as<gko::stop::ResidualNorm<TypeParam>>(factory_abs->generate(
@@ -1008,11 +1008,11 @@ TYPED_TEST(ImplicitResidualNorm, SimplifiedInterface)
10081008
std::shared_ptr<gko::LinOp> rhs = gko::initialize<Mtx>({10.0}, this->exec_);
10091009

10101010
auto factory_abs =
1011-
gko::stop::implicit_abs_residual_norm(0.5).on(this->exec_);
1011+
gko::stop::absolute_implicit_residual_norm(0.5).on(this->exec_);
10121012
auto factory_rel =
1013-
gko::stop::implicit_rel_residual_norm(0.5).on(this->exec_);
1013+
gko::stop::relative_implicit_residual_norm(0.5).on(this->exec_);
10141014
auto factory_red =
1015-
gko::stop::implicit_residual_norm_reduction(0.5).on(this->exec_);
1015+
gko::stop::initial_implicit_residual_norm(0.5).on(this->exec_);
10161016

10171017
auto crit_abs = gko::as<gko::stop::ImplicitResidualNorm<TypeParam>>(
10181018
factory_abs->generate(nullptr, rhs, initial_guess.get(),

reference/test/stop/time.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ TEST_F(Time, CanCreateFactory)
5454
}
5555

5656

57+
TEST_F(Time, CanCreateFactoryNewInterface)
58+
{
59+
auto new_factory =
60+
gko::stop::time_limit(std::chrono::milliseconds(test_ms)).on(exec_);
61+
ASSERT_NE(new_factory, nullptr);
62+
ASSERT_EQ(new_factory->get_parameters().time_limit,
63+
std::chrono::milliseconds(test_ms));
64+
}
65+
66+
5767
TEST_F(Time, CanCreateCriterion)
5868
{
5969
auto criterion = factory_->generate(nullptr, nullptr, nullptr);

test/mpi/solver/solver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ struct SimpleSolverTest {
7979
std::shared_ptr<const gko::Executor> exec)
8080
{
8181
return solver_type::build().with_criteria(
82-
gko::stop::iteration(iteration_count()),
83-
gko::stop::abs_residual_norm(reduction_factor()).on(exec));
82+
gko::stop::max_iters(iteration_count()),
83+
gko::stop::absolute_residual_norm(reduction_factor()).on(exec));
8484
}
8585

8686
static void assert_empty_state(const solver_type* mtx)

0 commit comments

Comments
 (0)