Skip to content

Commit

Permalink
figure out factorization test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Nov 12, 2024
1 parent ab98556 commit eb14467
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
5 changes: 4 additions & 1 deletion core/test/utils/assertions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,12 @@ template <typename MatrixData1, typename MatrixData2>
double get_relative_error(const MatrixData1& first, const MatrixData2& second)
{
using std::abs;
using vt = typename detail::biggest_valuetype<
using biggest_vt = typename detail::biggest_valuetype<
typename MatrixData1::value_type,
typename MatrixData2::value_type>::type;
// using the double or complex<double> to check the error
using vt = std::conditional_t<is_complex<biggest_vt>(),
std::complex<double>, double>;
using real_vt = remove_complex<vt>;

real_vt diff = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion test/factorization/cholesky_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ using Types = gko::test::ValueIndexTypes;
#elif defined(GKO_COMPILING_CUDA)
// CUDA doesn't support long indices for sorting, and the triangular solvers
// seem broken
using Types = gko::test::cartesian_type_product_t<gko::test::ValueTypes,
using Types = gko::test::cartesian_type_product_t<gko::test::ValueTypesWithHalf,
::testing::Types<gko::int32>>;
#else
// HIP only supports real types and int32
Expand Down
2 changes: 1 addition & 1 deletion test/factorization/lu_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ using Types = gko::test::ValueIndexTypesWithHalf;
#elif defined(GKO_COMPILING_CUDA)
// CUDA don't support long indices for sorting, and the triangular solvers
// seem broken
using Types = gko::test::cartesian_type_product_t<gko::test::ValueTypes,
using Types = gko::test::cartesian_type_product_t<gko::test::ValueTypesWithHalf,
::testing::Types<gko::int32>>;
#else
// HIP only supports real types and int32
Expand Down
15 changes: 14 additions & 1 deletion test/factorization/par_ic_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,29 @@ TYPED_TEST(ParIc, KernelComputeFactorIsEquivalentToRef)
using Csr = typename TestFixture::Csr;
using Coo = typename TestFixture::Coo;
using value_type = typename TestFixture::value_type;
#ifdef GKO_COMPILING_HIP
// hip does not support memory operation in 16bit
SKIP_IF_HALF(value_type);
#endif
auto square_size = this->mtx_ani->get_size();
auto mtx_l_coo = Coo::create(this->ref, square_size);
this->mtx_l_ani->convert_to(mtx_l_coo);
auto dmtx_l_coo = gko::clone(this->exec, mtx_l_coo);
// If we compute the mtx_near in half, we still get less 1e-4 in half
// precision By using double in mtx_near, we get around 2.4e-4.
// TODO: when gko::half support subnormal value, revisit this.
// Use the reference result as initial values in device::compute_factor, it
// still converges to the same result, which gives around 2.4e-4 against the
// reference result. Applying more iterations on the device side does not
// change the result. It might mean some values are subnormal such that both
// converges to different stable result.
auto tol = std::max(
1e-4, static_cast<double>(r<gko::remove_complex<value_type>>::value));

gko::kernels::reference::par_ic_factorization::compute_factor(
this->ref, 1, mtx_l_coo.get(), this->mtx_l_ani_init.get());
gko::kernels::GKO_DEVICE_NAMESPACE::par_ic_factorization::compute_factor(
this->exec, 100, dmtx_l_coo.get(), this->dmtx_l_ani_init.get());

GKO_ASSERT_MTX_NEAR(this->mtx_l_ani_init, this->dmtx_l_ani_init, 1e-4);
GKO_EXPECT_MTX_NEAR(this->mtx_l_ani_init, this->dmtx_l_ani_init, tol);
}
3 changes: 3 additions & 0 deletions test/factorization/par_ict_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ TYPED_TEST(ParIct, KernelComputeFactorIsEquivalentToRef)
using Csr = typename TestFixture::Csr;
using Coo = typename TestFixture::Coo;
using value_type = typename TestFixture::value_type;
#ifdef GKO_COMPILING_HIP
// hip does not support memory operation in 16bit
SKIP_IF_HALF(value_type);
#endif
auto square_size = this->mtx_ani->get_size();
auto mtx_l_coo = Coo::create(this->ref, square_size);
this->mtx_l_ani->convert_to(mtx_l_coo);
Expand Down
6 changes: 6 additions & 0 deletions test/factorization/par_ilu_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ TYPED_TEST(ParIlu, KernelComputeParILUIsEquivalentToRef)
{
using Csr = typename TestFixture::Csr;
using value_type = typename TestFixture::value_type;
#ifdef GKO_COMPILING_HIP
// hip does not support memory operation in 16bit
SKIP_IF_HALF(value_type);
#endif
std::unique_ptr<Csr> l_mtx{};
std::unique_ptr<Csr> u_mtx{};
std::unique_ptr<Csr> dl_mtx{};
Expand All @@ -257,7 +260,10 @@ TYPED_TEST(ParIlu, KernelComputeParILUWithMoreIterationsIsEquivalentToRef)
{
using Csr = typename TestFixture::Csr;
using value_type = typename TestFixture::value_type;
#ifdef GKO_COMPILING_HIP
// hip does not support memory operation in 16bit
SKIP_IF_HALF(value_type);
#endif
std::unique_ptr<Csr> l_mtx{};
std::unique_ptr<Csr> u_mtx{};
std::unique_ptr<Csr> dl_mtx{};
Expand Down
24 changes: 9 additions & 15 deletions test/factorization/par_ilut_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ class ParIlut : public CommonTestFixture {
mtx1 = gko::test::generate_random_matrix<Csr>(
mtx_size[0], mtx_size[1],
std::uniform_int_distribution<index_type>(10, mtx_size[1]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);
mtx2 = gko::test::generate_random_matrix<Csr>(
mtx_size[0], mtx_size[1],
std::uniform_int_distribution<index_type>(0, mtx_size[1]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);
mtx_square = gko::test::generate_random_matrix<Csr>(
mtx_size[0], mtx_size[0],
std::uniform_int_distribution<index_type>(1, mtx_size[0]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);
mtx_l = gko::test::generate_random_lower_triangular_matrix<Csr>(
mtx_size[0], false,
std::uniform_int_distribution<index_type>(10, mtx_size[0]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);
mtx_l2 = gko::test::generate_random_lower_triangular_matrix<Csr>(
mtx_size[0], true,
std::uniform_int_distribution<index_type>(1, mtx_size[0]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);
mtx_u = gko::test::generate_random_upper_triangular_matrix<Csr>(
mtx_size[0], false,
std::uniform_int_distribution<index_type>(10, mtx_size[0]),
std::normal_distribution<>(-1.0, 1.0), rand_engine, ref);
std::normal_distribution<>(0.0, 1.0), rand_engine, ref);

dmtx1 = gko::clone(exec, mtx1);
dmtx2 = gko::clone(exec, mtx2);
Expand Down Expand Up @@ -240,7 +240,6 @@ TYPED_TEST_SUITE(ParIlut, gko::test::ValueIndexTypesWithHalf,
TYPED_TEST(ParIlut, KernelThresholdSelectIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_select(this->mtx_l, this->dmtx_l,
this->mtx_l->get_num_stored_elements() / 3);
Expand All @@ -250,7 +249,6 @@ TYPED_TEST(ParIlut, KernelThresholdSelectIsEquivalentToRef)
TYPED_TEST(ParIlut, KernelThresholdSelectMinIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_select(this->mtx_l, this->dmtx_l, 0);
}
Expand All @@ -259,7 +257,6 @@ TYPED_TEST(ParIlut, KernelThresholdSelectMinIsEquivalentToRef)
TYPED_TEST(ParIlut, KernelThresholdSelectMaxIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_select(this->mtx_l, this->dmtx_l,
this->mtx_l->get_num_stored_elements() - 1);
Expand Down Expand Up @@ -327,7 +324,6 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef)
using Coo = typename TestFixture::Coo;
using value_type = typename TestFixture::value_type;
using index_type = typename TestFixture::index_type;
SKIP_IF_HALF(value_type);
this->test_filter(this->mtx_l, this->dmtx_l, 0.5, true);
auto res = Csr::create(this->ref, this->mtx_size);
auto dres = Csr::create(this->exec, this->mtx_size);
Expand All @@ -354,7 +350,6 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxNullptrCooIsEquivalentToRef)
TYPED_TEST(ParIlut, KernelThresholdFilterApproxLowerIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_filter_approx(this->mtx_l, this->dmtx_l,
this->mtx_l->get_num_stored_elements() / 2);
Expand All @@ -364,7 +359,6 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxLowerIsEquivalentToRef)
TYPED_TEST(ParIlut, KernelThresholdFilterApproxNoneLowerIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_filter_approx(this->mtx_l, this->dmtx_l, 0);
}
Expand All @@ -373,7 +367,6 @@ TYPED_TEST(ParIlut, KernelThresholdFilterApproxNoneLowerIsEquivalentToRef)
TYPED_TEST(ParIlut, KernelThresholdFilterApproxAllLowerIsEquivalentToRef)
{
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);

this->test_filter_approx(this->mtx_l, this->dmtx_l,
this->mtx_l->get_num_stored_elements() - 1);
Expand All @@ -384,8 +377,6 @@ TYPED_TEST(ParIlut, KernelAddCandidatesIsEquivalentToRef)
{
using Csr = typename TestFixture::Csr;
using value_type = typename TestFixture::value_type;
// there's one value larger than half range
SKIP_IF_HALF(value_type);
auto square_size = this->mtx_square->get_size();
auto mtx_lu = Csr::create(this->ref, square_size);
this->mtx_l2->apply(this->mtx_u, mtx_lu);
Expand Down Expand Up @@ -415,7 +406,10 @@ TYPED_TEST(ParIlut, KernelComputeLUIsEquivalentToRef)
using Csr = typename TestFixture::Csr;
using Coo = typename TestFixture::Coo;
using value_type = typename TestFixture::value_type;
#ifdef GKO_COMPILING_HIP
// hip does not support memory operation in 16bit
SKIP_IF_HALF(value_type);
#endif
auto square_size = this->mtx_ani->get_size();
auto mtx_l_coo = Coo::create(this->ref, square_size);
auto mtx_u_coo = Coo::create(this->ref, square_size);
Expand Down

0 comments on commit eb14467

Please sign in to comment.