Skip to content

Commit

Permalink
Use setters and getters as functions instead of a macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed May 15, 2020
1 parent b7b6c74 commit fad22d4
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 103 deletions.
32 changes: 2 additions & 30 deletions core/test/base/lin_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ class DummyLinOpWithFactory
GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
T GKO_FACTORY_PARAMETER(value, T{5});

gko::size_type GKO_FACTORY_PARAMETER(param, gko::size_type{2});
};
GKO_ENABLE_SET_GET_PARAMETERS(param, gko::size_type);
GKO_ENABLE_LIN_OP_FACTORY(DummyLinOpWithFactory, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

Expand All @@ -265,12 +262,9 @@ class DummyLinOpWithFactory
: gko::EnableLinOp<DummyLinOpWithFactory>(factory->get_executor()),
parameters_{factory->get_parameters()},
op_{op}
{
param_ = parameters_.param;
}
{}

std::shared_ptr<const gko::LinOp> op_;
gko::size_type param_;

protected:
void apply_impl(const gko::LinOp *b, gko::LinOp *x) const override {}
Expand All @@ -294,50 +288,28 @@ TEST_F(EnableLinOpFactory, CreatesDefaultFactory)
auto factory = DummyLinOpWithFactory<>::build().on(ref);

ASSERT_EQ(factory->get_parameters().value, 5);
ASSERT_EQ(factory->get_parameters().param, 2);
ASSERT_EQ(factory->get_executor(), ref);
}


TEST_F(EnableLinOpFactory, CreatesFactoryWithParameters)
{
auto factory =
DummyLinOpWithFactory<>::build().with_value(7).with_param(4u).on(ref);
auto factory = DummyLinOpWithFactory<>::build().with_value(7).on(ref);

ASSERT_EQ(factory->get_parameters().value, 7);
ASSERT_EQ(factory->get_parameters().param, 4);
ASSERT_EQ(factory->get_executor(), ref);
}


TEST_F(EnableLinOpFactory, PassesParametersToLinOp)
{
auto dummy = gko::share(DummyLinOp::create(ref, gko::dim<2>{3, 5}));
auto factory =
DummyLinOpWithFactory<>::build().with_value(6).with_param(7u).on(ref);

auto op = factory->generate(dummy);

ASSERT_EQ(op->get_executor(), ref);
ASSERT_EQ(op->get_parameters().value, 6);
ASSERT_EQ(op->get_parameters().param, 7);
ASSERT_EQ(op->get_param(), 7);
ASSERT_EQ(op->op_.get(), dummy.get());
}

TEST_F(EnableLinOpFactory, CanSetParameterLater)
{
auto dummy = gko::share(DummyLinOp::create(ref, gko::dim<2>{3, 5}));
auto factory = DummyLinOpWithFactory<>::build().with_value(6).on(ref);

auto op = factory->generate(dummy);
ASSERT_EQ(op->get_param(), 2);
gko::size_type p = 9u;
op->set_param(p);

ASSERT_EQ(op->get_executor(), ref);
ASSERT_EQ(op->get_parameters().value, 6);
ASSERT_EQ(op->get_param(), 9);
ASSERT_EQ(op->op_.get(), dummy.get());
}

Expand Down
34 changes: 0 additions & 34 deletions include/ginkgo/core/base/lin_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,40 +837,6 @@ public: \
"semi-colon warnings")


/**
* Defines setters and getters for the parameters of a class, simplifying its
* construction by removing the repetitive typing of boiler plate code. The
* setter and getter functions will be named set_parameter_name(...) and
* get_parameter_name().
*
* The passed in parameter should be a member variable of the class and not the
* factory parameter. These setters and getters can be used to re-set the member
* variables after the Factory has been generated. Hence, the generate step does
* not need to be called after using the setter in most cases when the
* _parameter_name_ member is modified in the apply_impl() of the class. But in
* case the _parameter_name_ is being set in either the Factory constructor or
* in the generate step as for the storage_optimization for the Jacobi
* preconditioner class, these setters/getters should *not* be used.
*
* @param _parameter_name the parameter whose setter and getter is to
* be defined
* @param _type the type of the parameter being passed in.
*
* @ingroup LinOp
*/
#define GKO_ENABLE_SET_GET_PARAMETERS(_parameter_name, _type) \
public: \
void set_##_parameter_name(_type other) \
{ \
this->_parameter_name##_ = other; \
} \
\
_type get_##_parameter_name() const { return this->_parameter_name##_; } \
static_assert(true, \
"This assert is used to counter the false positive extra " \
"semi-colon warnings")


/**
* Defines a build method for the factory, simplifying its construction by
* removing the repetitive typing of factory's name.
Expand Down
27 changes: 22 additions & 5 deletions include/ginkgo/core/solver/bicg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ class Bicg : public EnableLinOp<Bicg<ValueType>>, public Preconditionable {
*/
bool apply_uses_initial_guess() const override { return true; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand All @@ -116,11 +138,6 @@ class Bicg : public EnableLinOp<Bicg<ValueType>>, public Preconditionable {
GKO_ENABLE_LIN_OP_FACTORY(Bicg, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
27 changes: 22 additions & 5 deletions include/ginkgo/core/solver/bicgstab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ class Bicgstab : public EnableLinOp<Bicgstab<ValueType>>,
*/
bool apply_uses_initial_guess() const override { return true; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand All @@ -122,11 +144,6 @@ class Bicgstab : public EnableLinOp<Bicgstab<ValueType>>,
GKO_ENABLE_LIN_OP_FACTORY(Bicgstab, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
27 changes: 22 additions & 5 deletions include/ginkgo/core/solver/cg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ class Cg : public EnableLinOp<Cg<ValueType>>, public Preconditionable {
*/
bool apply_uses_initial_guess() const override { return true; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand All @@ -117,11 +139,6 @@ class Cg : public EnableLinOp<Cg<ValueType>>, public Preconditionable {
GKO_ENABLE_LIN_OP_FACTORY(Cg, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
27 changes: 22 additions & 5 deletions include/ginkgo/core/solver/cgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ class Cgs : public EnableLinOp<Cgs<ValueType>>, public Preconditionable {
*/
bool apply_uses_initial_guess() const override { return true; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand All @@ -114,11 +136,6 @@ class Cgs : public EnableLinOp<Cgs<ValueType>>, public Preconditionable {
GKO_ENABLE_LIN_OP_FACTORY(Cgs, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
27 changes: 22 additions & 5 deletions include/ginkgo/core/solver/fcg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ class Fcg : public EnableLinOp<Fcg<ValueType>>, public Preconditionable {
*/
bool apply_uses_initial_guess() const override { return true; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand All @@ -122,11 +144,6 @@ class Fcg : public EnableLinOp<Fcg<ValueType>>, public Preconditionable {
GKO_ENABLE_LIN_OP_FACTORY(Fcg, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
39 changes: 30 additions & 9 deletions include/ginkgo/core/solver/gmres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,41 @@ class Gmres : public EnableLinOp<Gmres<ValueType>>, public Preconditionable {
bool apply_uses_initial_guess() const override { return true; }

/**
* Returns the krylov dimension.
* Gets the krylov dimension of the solver
*
* @return the krylov dimension
*/
size_type get_krylov_dim() const { return krylov_dim_; }

/**
* Sets the krylov dimension
*
* @param other the new krylov dimension
*/
void set_krylov_dim(const size_type &other) { krylov_dim_ = other; }

/**
* Gets the stopping criterion factory of the solver.
*
* @return the stopping criterion factory
*/
std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
const
{
return stop_criterion_factory_;
}

/**
* Sets the stopping criterion of the solver.
*
* @param other the new stopping criterion factory
*/
void set_stop_criterion_factory(
std::shared_ptr<const stop::CriterionFactory> other)
{
stop_criterion_factory_ = std::move(other);
}

GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
Expand Down Expand Up @@ -129,14 +158,6 @@ class Gmres : public EnableLinOp<Gmres<ValueType>>, public Preconditionable {
GKO_ENABLE_LIN_OP_FACTORY(Gmres, parameters, Factory);
GKO_ENABLE_BUILD_METHOD(Factory);

// Enable setters and getters for the stop_criterion_factory member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(
stop_criterion_factory, std::shared_ptr<const stop::CriterionFactory>);
// Enable setters and getters for the krylov_dim member of this
// class.
GKO_ENABLE_SET_GET_PARAMETERS(krylov_dim, size_type);

protected:
void apply_impl(const LinOp *b, LinOp *x) const override;

Expand Down
Loading

0 comments on commit fad22d4

Please sign in to comment.