Skip to content

Commit

Permalink
matrix test with half
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Oct 29, 2024
1 parent ad40a31 commit e870b38
Show file tree
Hide file tree
Showing 32 changed files with 223 additions and 167 deletions.
3 changes: 2 additions & 1 deletion core/test/matrix/coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class Coo : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Coo, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Coo, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Coo, KnowsItsSize)
Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/coo_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CooBuilder : public ::testing::Test {
std::unique_ptr<Mtx> mtx;
};

TYPED_TEST_SUITE(CooBuilder, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(CooBuilder, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
3 changes: 2 additions & 1 deletion core/test/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class Csr : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Csr, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Csr, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Csr, KnowsItsSize)
Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/csr_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CsrBuilder : public ::testing::Test {
std::unique_ptr<Mtx> mtx;
};

TYPED_TEST_SUITE(CsrBuilder, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(CsrBuilder, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/dense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Dense : public ::testing::Test {
std::unique_ptr<gko::matrix::Dense<value_type>> mtx;
};

TYPED_TEST_SUITE(Dense, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Dense, gko::test::ValueTypesWithHalf, TypenameNameGenerator);


TYPED_TEST(Dense, CanBeEmpty)
Expand Down
3 changes: 2 additions & 1 deletion core/test/matrix/diagonal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Diagonal : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Diagonal, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Diagonal, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(Diagonal, KnowsItsSize)
Expand Down
3 changes: 2 additions & 1 deletion core/test/matrix/ell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class Ell : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Ell, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Ell, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Ell, KnowsItsSize)
Expand Down
7 changes: 4 additions & 3 deletions core/test/matrix/fbcsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class FbcsrSample : public ::testing::Test {
};


TYPED_TEST_SUITE(FbcsrSample, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(FbcsrSample, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down Expand Up @@ -183,7 +183,7 @@ template <typename ValueIndexType>
class FbcsrSampleComplex : public FbcsrSample<ValueIndexType> {};


TYPED_TEST_SUITE(FbcsrSampleComplex, gko::test::ComplexValueIndexTypes,
TYPED_TEST_SUITE(FbcsrSampleComplex, gko::test::ComplexValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down Expand Up @@ -282,7 +282,8 @@ class Fbcsr : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Fbcsr, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Fbcsr, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Fbcsr, GetNumBlocksCorrectlyThrows)
Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/fbcsr_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FbcsrBuilder : public ::testing::Test {
std::unique_ptr<Mtx> mtx;
};

TYPED_TEST_SUITE(FbcsrBuilder, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(FbcsrBuilder, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
3 changes: 2 additions & 1 deletion core/test/matrix/hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class Hybrid : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Hybrid, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Hybrid, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Hybrid, KnowsItsSize)
Expand Down
6 changes: 4 additions & 2 deletions core/test/matrix/identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Identity : public ::testing::Test {
std::shared_ptr<const gko::Executor> exec;
};

TYPED_TEST_SUITE(Identity, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Identity, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(Identity, CanBeEmpty)
Expand Down Expand Up @@ -81,7 +82,8 @@ class IdentityFactory : public ::testing::Test {
using value_type = T;
};

TYPED_TEST_SUITE(IdentityFactory, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(IdentityFactory, gko::test::ValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(IdentityFactory, CanGenerateIdentityMatrix)
Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Permutation : public ::testing::Test {
std::unique_ptr<gko::matrix::Permutation<index_type>> mtx;
};

TYPED_TEST_SUITE(Permutation, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(Permutation, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/row_gatherer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RowGatherer : public ::testing::Test {
std::unique_ptr<OutVec> out;
};

TYPED_TEST_SUITE(RowGatherer, gko::test::TwoValueIndexType,
TYPED_TEST_SUITE(RowGatherer, gko::test::TwoValueIndexTypeWithHalf,
TupleTypenameNameGenerator);


Expand Down
3 changes: 2 additions & 1 deletion core/test/matrix/sellp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ class Sellp : public ::testing::Test {
}
};

TYPED_TEST_SUITE(Sellp, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Sellp, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Sellp, KnowsItsSize)
Expand Down
2 changes: 1 addition & 1 deletion core/test/matrix/sparsity_csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SparsityCsr : public ::testing::Test {
}
};

TYPED_TEST_SUITE(SparsityCsr, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(SparsityCsr, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down
13 changes: 5 additions & 8 deletions core/test/utils/fb_matrix_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,15 @@ std::unique_ptr<matrix::Fbcsr<ValueType, IndexType>> generate_fbcsr_from_csr(
const IndexType* const row_ptrs = fmtx->get_const_row_ptrs();
const IndexType* const col_idxs = fmtx->get_const_col_idxs();
ValueType* const vals = fmtx->get_values();
std::uniform_real_distribution<gko::remove_complex<ValueType>>
off_diag_dist(-1.0, 1.0);
std::uniform_real_distribution<> off_diag_dist(-1.0, 1.0);

for (IndexType ibrow = 0; ibrow < nbrows; ibrow++) {
if (row_diag_dominant) {
const IndexType nrownz =
(row_ptrs[ibrow + 1] - row_ptrs[ibrow]) * block_size;

std::uniform_real_distribution<gko::remove_complex<ValueType>>
diag_dist(1.01 * nrownz, 2 * nrownz);
std::uniform_real_distribution<> diag_dist(1.01 * nrownz,
2 * nrownz);

for (IndexType ibz = row_ptrs[ibrow]; ibz < row_ptrs[ibrow + 1];
ibz++) {
Expand Down Expand Up @@ -205,13 +204,11 @@ std::unique_ptr<matrix::Fbcsr<ValueType, IndexType>> generate_random_fbcsr(
matrix::Csr<ValueType, IndexType>>(
nbrows, nbcols,
std::uniform_int_distribution<IndexType>(0, nbcols - 1),
std::normal_distribution<real_type>(0.0, 1.0),
std::move(engine), ref)
std::normal_distribution<>(0.0, 1.0), std::move(engine), ref)
: generate_random_matrix<matrix::Csr<ValueType, IndexType>>(
nbrows, nbcols,
std::uniform_int_distribution<IndexType>(0, nbcols - 1),
std::normal_distribution<real_type>(0.0, 1.0),
std::move(engine), ref);
std::normal_distribution<>(0.0, 1.0), std::move(engine), ref);
if (unsort && rand_csr_ref->is_sorted_by_column_index()) {
unsort_matrix(rand_csr_ref, engine);
}
Expand Down
6 changes: 4 additions & 2 deletions core/test/utils/value_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename ValueType, typename ValueDistribution, typename Engine>
typename std::enable_if<!is_complex_s<ValueType>::value, ValueType>::type
get_rand_value(ValueDistribution&& value_dist, Engine&& gen)
{
return value_dist(gen);
return static_cast<ValueType>(value_dist(gen));
}

/**
Expand All @@ -45,7 +45,9 @@ template <typename ValueType, typename ValueDistribution, typename Engine>
typename std::enable_if<is_complex_s<ValueType>::value, ValueType>::type
get_rand_value(ValueDistribution&& value_dist, Engine&& gen)
{
return ValueType(value_dist(gen), value_dist(gen));
using real_type = remove_complex<ValueType>;
return ValueType(static_cast<real_type>(value_dist(gen)),
static_cast<real_type>(value_dist(gen)));
}


Expand Down
56 changes: 38 additions & 18 deletions hip/test/matrix/fbcsr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <gtest/gtest.h>

#include <ginkgo/core/base/exception.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/matrix/fbcsr.hpp>

Expand Down Expand Up @@ -41,7 +42,7 @@ class Fbcsr : public HipTestFixture {

std::unique_ptr<const Mtx> rsorted_ref;

std::normal_distribution<gko::remove_complex<T>> distb;
std::normal_distribution<> distb;
std::default_random_engine engine;

value_type get_random_value()
Expand All @@ -60,7 +61,8 @@ class Fbcsr : public HipTestFixture {
}
};

TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(Fbcsr, CanWriteFromMatrixOnDevice)
Expand Down Expand Up @@ -145,11 +147,15 @@ TYPED_TEST(Fbcsr, SpmvIsEquivalentToRefSorted)
this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 1));
auto prod_hip = Dense::create(this->exec, prod_ref->get_size());

rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(x_hip, prod_hip), gko::NotImplemented);
} else {
rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand All @@ -169,11 +175,15 @@ TYPED_TEST(Fbcsr, SpmvMultiIsEquivalentToRefSorted)
this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 3));
auto prod_hip = Dense::create(this->exec, prod_ref->get_size());

rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(x_hip, prod_hip), gko::NotImplemented);
} else {
rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down Expand Up @@ -205,11 +215,16 @@ TYPED_TEST(Fbcsr, AdvancedSpmvIsEquivalentToRefSorted)
auto beta = Dense::create(this->exec);
beta->copy_from(beta_ref);

rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(alpha, x_hip, beta, prod_hip),
gko::NotImplemented);
} else {
rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down Expand Up @@ -241,11 +256,16 @@ TYPED_TEST(Fbcsr, AdvancedSpmvMultiIsEquivalentToRefSorted)
auto beta = Dense::create(this->exec);
beta->copy_from(beta_ref);

rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(alpha, x_hip, beta, prod_hip),
gko::NotImplemented);
} else {
rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down
Loading

0 comments on commit e870b38

Please sign in to comment.