Skip to content

Commit

Permalink
review updates
Browse files Browse the repository at this point in the history
* fix missing documentation
* fix mixed precision tests
* add tests for device temporary conversion
* add tests for make_temporary_conversion behavior

Co-authored-by: Aditya Kashi <aditya.kashi@kit.edu>
  • Loading branch information
upsj and Slaedr committed Mar 30, 2021
1 parent e82a83b commit 023fbd8
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 13 deletions.
13 changes: 13 additions & 0 deletions core/test/base/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ TEST_F(TemporaryClone, CopiesBackAfterLeavingScope)
}


TEST_F(TemporaryClone, DoesntCopyBackConstAfterLeavingScope)
{
{
auto clone = make_temporary_clone(
omp, static_cast<const DummyObject *>(gko::lend(obj)));
obj->data = 7;
}

ASSERT_EQ(obj->get_executor(), ref);
ASSERT_EQ(obj->data, 7);
}


TEST_F(TemporaryClone, AvoidsCopyOnSameExecutor)
{
auto clone = make_temporary_clone(ref, gko::lend(obj));
Expand Down
71 changes: 71 additions & 0 deletions cuda/test/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ class Dense : public ::testing::Test {
using itype = int;
using vtype = double;
using Mtx = gko::matrix::Dense<vtype>;
using MixedMtx = gko::matrix::Dense<gko::next_precision<vtype>>;
using NormVector = gko::matrix::Dense<gko::remove_complex<vtype>>;
using Arr = gko::Array<itype>;
using ComplexMtx = gko::matrix::Dense<std::complex<vtype>>;
using MixedComplexMtx =
gko::matrix::Dense<gko::next_precision<std::complex<vtype>>>;

Dense() : rand_engine(15) {}

Expand Down Expand Up @@ -154,6 +157,14 @@ class Dense : public ::testing::Test {
std::unique_ptr<Arr>(new Arr{ref, tmp3.begin(), tmp3.end()});
}

template <typename ConvertedType, typename InputType>
std::unique_ptr<ConvertedType> convert(InputType &&input)
{
auto result = ConvertedType::create(input->get_executor());
input->convert_to(result.get());
return result;
}

std::shared_ptr<gko::ReferenceExecutor> ref;
std::shared_ptr<const gko::CudaExecutor> cuda;

Expand Down Expand Up @@ -347,6 +358,17 @@ TEST_F(Dense, SimpleApplyIsEquivalentToRef)
}


TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(y).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dy).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7);
}


TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -358,6 +380,19 @@ TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(alpha).get(), convert<MixedMtx>(y).get(),
convert<MixedMtx>(beta).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dalpha).get(), convert<MixedMtx>(dy).get(),
convert<MixedMtx>(dbeta).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
}


TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -375,6 +410,23 @@ TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(cuda);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(65, 1);
auto dcomplex_x = MixedComplexMtx::create(cuda);
dcomplex_x->copy_from(complex_x.get());

x->apply(complex_b.get(), complex_x.get());
dx->apply(dcomplex_b.get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -392,6 +444,25 @@ TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(cuda);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(65, 1);
auto dcomplex_x = MixedComplexMtx::create(cuda);
dcomplex_x->copy_from(complex_x.get());

x->apply(convert<MixedMtx>(alpha).get(), complex_b.get(),
convert<MixedMtx>(beta).get(), complex_x.get());
dx->apply(convert<MixedMtx>(dalpha).get(), dcomplex_b.get(),
convert<MixedMtx>(dbeta).get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, IsTransposable)
{
set_up_apply_data();
Expand Down
71 changes: 71 additions & 0 deletions hip/test/matrix/dense_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ class Dense : public ::testing::Test {
using itype = int;
using vtype = double;
using Mtx = gko::matrix::Dense<vtype>;
using MixedMtx = gko::matrix::Dense<gko::next_precision<vtype>>;
using NormVector = gko::matrix::Dense<gko::remove_complex<vtype>>;
using Arr = gko::Array<itype>;
using ComplexMtx = gko::matrix::Dense<std::complex<vtype>>;
using MixedComplexMtx =
gko::matrix::Dense<gko::next_precision<std::complex<vtype>>>;

Dense() : rand_engine(15) {}

Expand Down Expand Up @@ -151,6 +154,14 @@ class Dense : public ::testing::Test {
std::unique_ptr<Arr>(new Arr{ref, tmp3.begin(), tmp3.end()});
}

template <typename ConvertedType, typename InputType>
std::unique_ptr<ConvertedType> convert(InputType &&input)
{
auto result = ConvertedType::create(input->get_executor());
input->convert_to(result.get());
return result;
}

std::shared_ptr<gko::ReferenceExecutor> ref;
std::shared_ptr<const gko::HipExecutor> hip;

Expand Down Expand Up @@ -342,6 +353,17 @@ TEST_F(Dense, SimpleApplyIsEquivalentToRef)
}


TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(y).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dy).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7);
}


TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -353,6 +375,19 @@ TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(alpha).get(), convert<MixedMtx>(y).get(),
convert<MixedMtx>(beta).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dalpha).get(), convert<MixedMtx>(dy).get(),
convert<MixedMtx>(dbeta).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
}


TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -370,6 +405,23 @@ TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(hip);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(65, 1);
auto dcomplex_x = MixedComplexMtx::create(hip);
dcomplex_x->copy_from(complex_x.get());

x->apply(complex_b.get(), complex_x.get());
dx->apply(dcomplex_b.get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -387,6 +439,25 @@ TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(hip);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(65, 1);
auto dcomplex_x = MixedComplexMtx::create(hip);
dcomplex_x->copy_from(complex_x.get());

x->apply(convert<MixedMtx>(alpha).get(), complex_b.get(),
convert<MixedMtx>(beta).get(), complex_x.get());
dx->apply(convert<MixedMtx>(dalpha).get(), dcomplex_b.get(),
convert<MixedMtx>(dbeta).get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, IsTransposable)
{
set_up_apply_data();
Expand Down
2 changes: 1 addition & 1 deletion include/ginkgo/core/base/temporary_conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class convert_back_deleter<const CopyType, const OrigType> {
*
* Helper type that attempts to statically find the dynamic type of a given
* LinOp from a list of ConversionCandidates and, on the first match, converts
* it to TargetType with an appropriate .
* it to TargetType with an appropriate convert_back_deleter.
*
* @tparam ConversionCandidates list of potential dynamic types of the input
* object to be checked.
Expand Down
81 changes: 77 additions & 4 deletions omp/test/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ namespace {

class Dense : public ::testing::Test {
protected:
using Mtx = gko::matrix::Dense<>;
using NormVector = gko::matrix::Dense<gko::remove_complex<Mtx::value_type>>;
using Arr = gko::Array<int>;
using ComplexMtx = gko::matrix::Dense<std::complex<double>>;
using itype = int;
using vtype = double;
using Mtx = gko::matrix::Dense<vtype>;
using MixedMtx = gko::matrix::Dense<gko::next_precision<vtype>>;
using NormVector = gko::matrix::Dense<gko::remove_complex<vtype>>;
using Arr = gko::Array<itype>;
using ComplexMtx = gko::matrix::Dense<std::complex<vtype>>;
using MixedComplexMtx =
gko::matrix::Dense<gko::next_precision<std::complex<vtype>>>;

Dense() : rand_engine(15) {}

Expand Down Expand Up @@ -166,6 +171,14 @@ class Dense : public ::testing::Test {
std::unique_ptr<Arr>(new Arr{ref, tmp3.begin(), tmp3.end()});
}

template <typename ConvertedType, typename InputType>
std::unique_ptr<ConvertedType> convert(InputType &&input)
{
auto result = ConvertedType::create(input->get_executor());
input->convert_to(result.get());
return result;
}

std::shared_ptr<gko::ReferenceExecutor> ref;
std::shared_ptr<const gko::OmpExecutor> omp;

Expand Down Expand Up @@ -359,6 +372,17 @@ TEST_F(Dense, SimpleApplyIsEquivalentToRef)
}


TEST_F(Dense, SimpleApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(y).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dy).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-7);
}


TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -370,6 +394,19 @@ TEST_F(Dense, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyMixedIsEquivalentToRef)
{
set_up_apply_data();

x->apply(convert<MixedMtx>(alpha).get(), convert<MixedMtx>(y).get(),
convert<MixedMtx>(beta).get(), convert<MixedMtx>(expected).get());
dx->apply(convert<MixedMtx>(dalpha).get(), convert<MixedMtx>(dy).get(),
convert<MixedMtx>(dbeta).get(), convert<MixedMtx>(dresult).get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
}


TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -387,6 +424,23 @@ TEST_F(Dense, ApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, ApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(omp);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(40, 1);
auto dcomplex_x = MixedComplexMtx::create(omp);
dcomplex_x->copy_from(complex_x.get());

x->apply(complex_b.get(), complex_x.get());
dx->apply(dcomplex_b.get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -404,6 +458,25 @@ TEST_F(Dense, AdvancedApplyToComplexIsEquivalentToRef)
}


TEST_F(Dense, AdvancedApplyToMixedComplexIsEquivalentToRef)
{
set_up_apply_data();
auto complex_b = gen_mtx<MixedComplexMtx>(25, 1);
auto dcomplex_b = MixedComplexMtx::create(omp);
dcomplex_b->copy_from(complex_b.get());
auto complex_x = gen_mtx<MixedComplexMtx>(40, 1);
auto dcomplex_x = MixedComplexMtx::create(omp);
dcomplex_x->copy_from(complex_x.get());

x->apply(convert<MixedMtx>(alpha).get(), complex_b.get(),
convert<MixedMtx>(beta).get(), complex_x.get());
dx->apply(convert<MixedMtx>(dalpha).get(), dcomplex_b.get(),
convert<MixedMtx>(dbeta).get(), dcomplex_x.get());

GKO_ASSERT_MTX_NEAR(dcomplex_x, complex_x, 1e-7);
}


TEST_F(Dense, ConvertToCooIsEquivalentToRef)
{
auto rmtx = gen_mtx<Mtx>(532, 231);
Expand Down
Loading

0 comments on commit 023fbd8

Please sign in to comment.