Skip to content

Commit

Permalink
rename turn_absolute and add interface for LinOp
Browse files Browse the repository at this point in the history
1. rename turn_absolute -> apply_absolute
2. add AbsoluteComputable for LinOp usage
  • Loading branch information
yhmtsai committed Sep 9, 2020
1 parent eb0c320 commit f0d4234
Show file tree
Hide file tree
Showing 45 changed files with 236 additions and 117 deletions.
2 changes: 1 addition & 1 deletion core/matrix/coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Coo<ValueType, IndexType>::extract_diagonal() const


template <typename ValueType, typename IndexType>
void Coo<ValueType, IndexType>::turn_absolute()
void Coo<ValueType, IndexType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ Csr<ValueType, IndexType>::extract_diagonal() const


template <typename ValueType, typename IndexType>
void Csr<ValueType, IndexType>::turn_absolute()
void Csr<ValueType, IndexType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/dense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ std::unique_ptr<Diagonal<ValueType>> Dense<ValueType>::extract_diagonal() const


template <typename ValueType>
void Dense<ValueType>::turn_absolute()
void Dense<ValueType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/diagonal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void Diagonal<ValueType>::write(mat_data32 &data) const


template <typename ValueType>
void Diagonal<ValueType>::turn_absolute()
void Diagonal<ValueType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/ell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Ell<ValueType, IndexType>::extract_diagonal() const


template <typename ValueType, typename IndexType>
void Ell<ValueType, IndexType>::turn_absolute()
void Ell<ValueType, IndexType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ Hybrid<ValueType, IndexType>::extract_diagonal() const


template <typename ValueType, typename IndexType>
void Hybrid<ValueType, IndexType>::turn_absolute()
void Hybrid<ValueType, IndexType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
2 changes: 1 addition & 1 deletion core/matrix/sellp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Sellp<ValueType, IndexType>::extract_diagonal() const


template <typename ValueType, typename IndexType>
void Sellp<ValueType, IndexType>::turn_absolute()
void Sellp<ValueType, IndexType>::apply_absolute()
{
auto exec = this->get_executor();

Expand Down
102 changes: 101 additions & 1 deletion core/test/base/lin_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <complex>
#include <ginkgo/core/base/lin_op.hpp>

#include <ginkgo/core/base/math.hpp>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -314,4 +315,103 @@ TEST_F(EnableLinOpFactory, PassesParametersToLinOp)
}


template <typename Type>
class DummyLinOpWithType
: public gko::EnableLinOp<DummyLinOpWithType<Type>>,
public gko::EnableCreateMethod<DummyLinOpWithType<Type>>,
public gko::EnableAbsoluteComputation<DummyLinOpWithType<Type>> {
public:
using outplace_absolute_type = gko::remove_complex<DummyLinOpWithType>;
DummyLinOpWithType(std::shared_ptr<const gko::Executor> exec)
: gko::EnableLinOp<DummyLinOpWithType>(exec)
{}

DummyLinOpWithType(std::shared_ptr<const gko::Executor> exec,
gko::dim<2> size, Type value)
: gko::EnableLinOp<DummyLinOpWithType>(exec, size), value_(value)
{}

void apply_absolute() override { value_ = gko::abs(value_); }

std::unique_ptr<outplace_absolute_type> get_absolute() const override
{
return std::make_unique<outplace_absolute_type>(
this->get_executor(), this->get_size(), gko::abs(value_));
}

Type get_value() const { return value_; }

protected:
void apply_impl(const gko::LinOp *b, gko::LinOp *x) const override {}

void apply_impl(const gko::LinOp *alpha, const gko::LinOp *b,
const gko::LinOp *beta, gko::LinOp *x) const override
{}

private:
Type value_;
};


class EnableAbsoluteComputation : public ::testing::Test {
protected:
using dummy_type = DummyLinOpWithType<std::complex<double>>;
EnableAbsoluteComputation()
: ref{gko::ReferenceExecutor::create()},
op{dummy_type::create(ref, gko::dim<2>{1, 1},
std::complex<double>{-3.0, 4.0})}
{}

std::shared_ptr<const gko::ReferenceExecutor> ref;
std::shared_ptr<dummy_type> op;
};


TEST_F(EnableAbsoluteComputation, InplaceAbsoluteOnConcreteType)
{
op->apply_absolute();

ASSERT_EQ(op->get_value(), std::complex<double>{5.0});
}


TEST_F(EnableAbsoluteComputation, OutplaceAbsoluteOnConcreteType)
{
auto abs_op = op->get_absolute();

ASSERT_EQ(typeid(abs_op),
typeid(std::unique_ptr<gko::remove_complex<dummy_type>>));
ASSERT_EQ(abs_op->get_value(), 5.0);
}


TEST_F(EnableAbsoluteComputation, InplaceAbsoluteOnAbsoluteComputable)
{
auto linop = gko::as<gko::LinOp>(op);

gko::as<gko::AbsoluteComputable>(linop)->apply_absolute();

ASSERT_EQ(gko::as<dummy_type>(linop)->get_value(),
std::complex<double>{5.0});
}


TEST_F(EnableAbsoluteComputation, OutplaceAbsoluteOnAbsoluteComputable)
{
auto abs_op = op->get_absolute();

ASSERT_EQ(typeid(abs_op),
typeid(std::unique_ptr<gko::remove_complex<dummy_type>>));
ASSERT_EQ(abs_op->get_value(), 5.0);
}


TEST_F(EnableAbsoluteComputation, ThrowWithoutAbsoluteComputableInterface)
{
std::shared_ptr<gko::LinOp> linop = DummyLinOp::create(ref);

ASSERT_THROW(gko::as<gko::AbsoluteComputable>(linop), gko::NotSupported);
}


} // namespace
4 changes: 2 additions & 2 deletions cuda/test/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
8 changes: 4 additions & 4 deletions cuda/test/matrix/csr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,8 @@ TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data(std::make_shared<Mtx::automatical>(cuda));

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand All @@ -766,8 +766,8 @@ TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef)
{
set_up_apply_complex_data(std::make_shared<ComplexMtx::automatical>(cuda));

complex_mtx->turn_absolute();
complex_dmtx->turn_absolute();
complex_mtx->apply_absolute();
complex_dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(complex_mtx.get(), complex_dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions cuda/test/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ TEST_F(Dense, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

x->turn_absolute();
dx->turn_absolute();
x->apply_absolute();
dx->apply_absolute();

GKO_ASSERT_MTX_NEAR(x.get(), dx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions cuda/test/matrix/diagonal_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

diag->turn_absolute();
ddiag->turn_absolute();
diag->apply_absolute();
ddiag->apply_absolute();

GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions cuda/test/matrix/ell_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions cuda/test/matrix/hybrid_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions cuda/test/matrix/sellp_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_matrix(32, 2);

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/coo_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ TEST_F(Coo, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
8 changes: 4 additions & 4 deletions hip/test/matrix/csr_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ TEST_F(Csr, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data(std::make_shared<Mtx::automatical>(hip));

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand All @@ -751,8 +751,8 @@ TEST_F(Csr, InplaceAbsoluteComplexMatrixIsEquivalentToRef)
{
set_up_apply_complex_data(std::make_shared<ComplexMtx::automatical>(hip));

complex_mtx->turn_absolute();
complex_dmtx->turn_absolute();
complex_mtx->apply_absolute();
complex_dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(complex_mtx.get(), complex_dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/dense_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ TEST_F(Dense, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

x->turn_absolute();
dx->turn_absolute();
x->apply_absolute();
dx->apply_absolute();

GKO_ASSERT_MTX_NEAR(x.get(), dx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/diagonal_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ TEST_F(Diagonal, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

diag->turn_absolute();
ddiag->turn_absolute();
diag->apply_absolute();
ddiag->apply_absolute();

GKO_ASSERT_MTX_NEAR(diag.get(), ddiag.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/ell_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ TEST_F(Ell, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/hybrid_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ TEST_F(Hybrid, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_data();

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
4 changes: 2 additions & 2 deletions hip/test/matrix/sellp_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ TEST_F(Sellp, InplaceAbsoluteMatrixIsEquivalentToRef)
{
set_up_apply_matrix(32, 2);

mtx->turn_absolute();
dmtx->turn_absolute();
mtx->apply_absolute();
dmtx->apply_absolute();

GKO_ASSERT_MTX_NEAR(mtx.get(), dmtx.get(), 1e-14);
}
Expand Down
Loading

0 comments on commit f0d4234

Please sign in to comment.